UTF-8问题
问题简述:汉字字符串在gb2312和utf-8编码之间的转换,我是用了类似于gb2312和iso-8859-1编码转化的方法,结果对中文字符无效
参考以下例子程序:
public class test {
public static void main(String[] args) {
toUTF8("abc");
toUTF8("汉字");
}
static void toUTF8(String s){
System.out.println("Source string: "+s);
try{
s = new String(s.getBytes(), "UTF8");
byte[] bytes = s.getBytes("UTF8");
System.out.println("length: "+bytes.length);
}catch(java.io.UnsupportedEncodingException uee){
uee.printStackTrace();
}
}
}
输出:
Source string: abc
length: 3
Source string: 汉字
length: 0
我发现中文string从gb2312转化到utf-8后,成了一个空string
中文win2000+jb6,系统默认编码GBK