编码转换!
/**
* @author yshlin
* @version 1.0
* E-mail:yshlin1106@126.com
*/
import java.util.StringTokenizer;
public class CodeFuns {
public CodeFuns() {
}
public String toEncoding(String strCh) {
String strEnch = strCh;
byte[] byStr;
try {
byStr = strEnch.getBytes("iso-8859-1");
strEnch = new String(byStr);
} catch (Exception e1) {
System.out.println("Exception:" + e1);
}
return strEnch;
}
public String replaceAll(String source, String a, String b) { //得到最终替换的结果
String cont = processSpace(a, b, source);
return cont;
}
public String processSpace(String from, String to, String source) {
StringBuffer bf = new StringBuffer();
StringTokenizer st = new StringTokenizer(source, from, true);
while (st.hasMoreTokens()) {
String tmp = st.nextToken();
if (tmp.equals(from)) {
bf.append(to);
} else {
bf.append(tmp);
}
}
return bf.toString();
}
public String toChinese(String strCh) {
String strChEn = strCh;
try {
strChEn = new String(strChEn.getBytes("iso-8859-1"), "GB2312");
} catch (Exception e2) {
System.out.println("Exception:" + e2);
}
return strChEn;
}
/**
* 功能:iso-8859-1转换为gb2312
* @author renwy
* @param qs String
* @return String
*/
public String iso2gb(String qs) {
try {
if (qs == null)
return "NULL";
else {
return new String(qs.getBytes("iso-8859-1"), "gb2312");
}
} catch (Exception e) {
System.err.println("iso2gb error:" + e.getMessage());
e.printStackTrace();
}
return "NULL";
}
/**
* 功能:gb2312转换为iso-8859-1
* @author renwy
* @param qs String
* @return String
*/
public String gb2iso(String gb) {
try {
if (gb == null)
return "NULL";
else {
return new String(gb.getBytes("gb2312"), "iso-8859-1");
}
} catch (Exception e) {
System.err.println("gb2iso error:" + e.getMessage());
e.printStackTrace();
}
return "NULL";
}
}