原理:
已unicode为中介
utf-8 ——》unicode——》gbk
gbk ——》unicode——》utf-8
知识点:
1、 unicode是一种“编码”,
所谓编码就是一个编号(数字)到字符的一种映射关系,
就仅仅是一种一对一的映射而已,
可以理解成一个很大的对应表格。
2、 GBK、UTF-8是一种“编码格式”,
是用来序列化或存储1中提到的那个“编号(数字)”的一种“格式”;
GBK和UTF-8都是用来序列化或存储unicode编码的数据的,
但是分别是2种不同的格式;
他们俩除了格式不一样之外,
他们所关心的unicode编码范围也不一样,
utf-8考虑了很多种不同国家的字符,
涵盖整个unicode码表,
所以其存储一个字符的编码的时候,
使用的字节长度也从1字节到4字节不等;
而GBK只考虑中文——在unicode中的一小部分——的字符的编码,
所以它算好了只要2个字节就能涵盖到绝大多数常用中文(2个字节能表示6w多种字符),
所以它存储一个字符的时候,
所用的字节长度是固定的;
链接:https://www.zhihu.com/question/20361462/answer/14899233
正确代码
方法1:
//utf-8转gbk
String clientStr = new String(str.getBytes("GBK"), "GBK");
//gbk转utf-8
String clientStr = new String(str.getBytes("UTF-8"), "UTF-8");
方法2:
/**
* gbk与utf-8互转
* 利用BASE64Encoder/BASE64Decoder实现互转
* @param str
* @return
*/
private String charsetConvert(String str, String charset) {
try {
str = new sun.misc.BASE64Encoder().encode(str.getBytes(charset));
byte[] bytes = new sun.misc.BASE64Decoder().decodeBuffer(str);
str = new String(bytes, charset);
} catch(IOException e) {
e.printStackTrace();
}
return str;
}
错误代码
/**
* gbk转utf-8
* @param str
* @return
*/
public static String gbkToUtf8(String str) {
String utf8Str = "";
try {
String UnicodeStr = gbkToUnicode(str);
utf8Str = unicodeToUtf8(UnicodeStr);
} catch (Exception e) {
LogUtil.info("gbk转utf-8 异常,原字符串返回!");
utf8Str = str;
}
return utf8Str;
}
/**
* utf-8转gbk,实际测试中发现有部分汉字转换不了
* @param str
* @return
*/
public static String utf8ToGbk(String str) {
String gbkStr = "";
try {
String UnicodeStr = utf8ToUnicode(str);
gbkStr = unicodeToGbk(UnicodeStr);
} catch (Exception e) {
LogUtil.info("utf-8 转 gbk异常,原字符串返回!");
gbkStr = str;
}
return gbkStr;
}
上面utf-8转gbk,实际测试中发现有部分汉字转换不了
/**
* gbk转unicode
* @param str
* @return
*/
public static String gbkToUnicode(String str) {
StringBuffer result = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
char chr1 = (char) str.charAt(i);
if ((chr1 & (0x00FF)) == chr1) {
result.append(chr1);
continue;
}
result.append("\\u" + Integer.toHexString((int) chr1));
}
return result.toString();
}
/**
* unicode转gbk
* @param dataStr
* @return
*/
public static String unicodeToGbk(String dataStr) {
int index = 0;
StringBuffer buffer = new StringBuffer();
int li_len = dataStr.length();
while (index < li_len) {
if (index >= li_len - 1
|| !"\\u".equals(dataStr.substring(index, index + 2))) {
buffer.append(dataStr.charAt(index));
index++;
continue;
}
String charStr = "";
charStr = dataStr.substring(index + 2, index + 6);
char letter = (char) Integer.parseInt(charStr, 16);
buffer.append(letter);
index += 6;
}
return buffer.toString();
}
/**
* utf-8转unicode
* @param inStr
* @return
*/
public static String utf8ToUnicode(String inStr) {
char[] myBuffer = inStr.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < inStr.length(); i++) {
UnicodeBlock ub = UnicodeBlock.of(myBuffer[i]);
if (ub == UnicodeBlock.BASIC_LATIN) {
sb.append(myBuffer[i]);
} else if (ub == UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
int j = (int) myBuffer[i] - 65248;
sb.append((char) j);
} else {
short s = (short) myBuffer[i];
String hexS = Integer.toHexString(s);
String unicode = "\\u" + hexS;
sb.append(unicode.toLowerCase());
}
}
return sb.toString();
}
/**
* unicode转utf-8
* @param theString
* @return
*/
public static String unicodeToUtf8(String theString) {
char aChar;
int len = theString.length();
StringBuffer outBuffer = new StringBuffer(len);
for (int x = 0; x < len;) {
aChar = theString.charAt(x++);
if (aChar == '\\') {
aChar = theString.charAt(x++);
if (aChar == 'u') {
int value = 0;
for (int i = 0; i < 4; i++) {
aChar = theString.charAt(x++);
switch (aChar) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
value = (value << 4) + aChar - '0';
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
value = (value << 4) + 10 + aChar - 'a';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
value = (value << 4) + 10 + aChar - 'A';
break;
default:
throw new IllegalArgumentException(
"Malformed \\uxxxx encoding.");
}
}
outBuffer.append((char) value);
} else {
if (aChar == 't')
aChar = '\t';
else if (aChar == 'r')
aChar = '\r';
else if (aChar == 'n')
aChar = '\n';
else if (aChar == 'f')
aChar = '\f';
outBuffer.append(aChar);
}
} else
outBuffer.append(aChar);
}
return outBuffer.toString();
}
参照:原博客