/**
* gb2312转utf8,用于手机显示
* @param gbString String
* @return String
*/
public static String GB2UTF8(final String gbString)
{
if (gbString == null)
return "";
char[] utfBytes = gbString.toCharArray();
String unicodeBytes = "";
for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {
if (utfBytes[byteIndex] >= '!' && utfBytes[byteIndex] <= '~'){
unicodeBytes += utfBytes[byteIndex];
}else {
String hexB = Integer.toHexString(utfBytes[byteIndex]);
if (hexB.length() <= 2) {
<a id="more-7"></a>
hexB = "00" + hexB;
}
unicodeBytes = unicodeBytes + "" + hexB + ";";
}
}
return unicodeBytes;
}
/**
* utf8 转 gb2312 ,用于手机显示
* @param strValue String
* @return String
*/
public static String UTF82GB(String strValue){
if(strValue==null || strValue.trim().length()==0)
{
return null;
}
StringBuffer strbuf = new StringBuffer();
int pos = 0;
String[] strarr = strValue.split(";");
for(int i=0; i<strarr.length; i++){
pos = strarr[i].indexOf("");
if(pos >= 0){
String tmp = strarr[i].substring(pos + 3);
if (tmp.startsWith("00")) {
tmp = tmp.substring(2);
}
int l = Integer.valueOf(tmp, 16).intValue();
strbuf.append( (char) l);
}else{
strbuf.append(strarr[i]);
}
}
return strbuf.toString();
}
返回