JAVA怎么把unicode转换成GBK ~然后我的JAVA代码里出现了乱码导致运行不了,你能帮我吗?

2025-01-05 19:05:50
推荐回答(3个)
回答1:

\u2605 ★

public static String utf8ToUnicode(String str) {
char[] myBuffer = str.toCharArray();

StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.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();
}

回答2:

项目里面的么?那就在项目上右键选最下面那个propertis 然后选Resource 有个text encoding。。。选你要的编码方式么。

回答3:

new String(new String("中国").getBytes(),"GBK")