字符串编码转换小结1
基础:
public String changeCharset(String str)
throws UnsupportedEncodingException {
if (str != null) {
//得到gbk编码的字节串
byte[] bs = str.getBytes("GBK");
System.out.println("gbk:");
System.out.println("hex format is:"+encodeHex(bs));
//得到utf8编码的字节串
byte[] ns = str.getBytes("UTF-8");
System.out.println("utf8:");
System.out.println("hex format is:"+encodeHex(ns));
System.out.println("Bin format is:"+encodeBin(ns)
);
//将编码为utf8的字节串再生成新的字符串,然后解码,看看转换过程中是否出错
String newutf=new String(ns,"UTF-8");
System.out.println("convert back from utf8:"+encodeHex(newutf.getBytes("UTF-8")));
return newutf;
}
public static final String encodeHex (byte[] bytes)
{
StringBuffer buff =
new StringBuffer(bytes.length * 2);
String b;
for (int i=0; i<bytes.length ; i++)
{
b = Integer.toHexString(bytes[i]);
// byte是两个字节的, 而上面的Integer.toHexString会把字节扩展为4个字节
buff.append(b.length() > 2 ? b.substring(6,8) : b);
buff.append(" ");
}
return buff.toString();
}
public static final String encodeBin (byte[] bytes)
{
StringBuffer buff =
new StringBuffer(bytes.length * 2);
String b;
for (int i=0; i<bytes.length ; i++)
{
b = Integer.toBinaryString(bytes[i]);
// byte是两个字节的, 而上面的Integer.toHexString会把字节扩展为4个字节
buff.append(b.substring(24,b.length()));
buff.append(" ");
}
return buff.toString();
}
在Java中,String.getBytes("GBK")获得的是gbk编码的字节串,打印出来之后得到的是对应编码的十进制值,转换成16进制后就和编码表中的值一样了。
最后可以根据输出的二进制编码,和unicode表对比。
示例:
str: 中国
gbk:
hex format is:d6 d0 b9 fa
utf8:
hex format is:e4 b8 ad e5 9b bd
Bin format is:11100100 10111000 10101101 11100101 10011011 10111101
convert back from utf8:e4 b8 ad e5 9b bd
其中,“中”的unicode编码是4e2d,二进制表示为0100 1110 0010 1101,从低位到高位按六位截取及补齐高位后就可以得到11100100 10111000 1010110 ,即为utf8编码。
中文编码基础知识介绍(理论基础)
Java 编程技术中汉字问题的分析及解决
String.getBytes()方法中的中文编码问题 (这篇最为简单易懂且结合实际)
Unicode和UTF-8之间的转换详解
附录:
unicode编码表
GBK 汉字内码扩展规范编码表(二)
代码示例:public String changeCharset(String str)
throws UnsupportedEncodingException {
if (str != null) {
//得到gbk编码的字节串
byte[] bs = str.getBytes("GBK");
System.out.println("gbk:");
System.out.println("hex format is:"+encodeHex(bs));
//得到utf8编码的字节串
byte[] ns = str.getBytes("UTF-8");
System.out.println("utf8:");
System.out.println("hex format is:"+encodeHex(ns));
System.out.println("Bin format is:"+encodeBin(ns)
);
//将编码为utf8的字节串再生成新的字符串,然后解码,看看转换过程中是否出错
String newutf=new String(ns,"UTF-8");
System.out.println("convert back from utf8:"+encodeHex(newutf.getBytes("UTF-8")));
return newutf;
}
public static final String encodeHex (byte[] bytes)
{
StringBuffer buff =
new StringBuffer(bytes.length * 2);
String b;
for (int i=0; i<bytes.length ; i++)
{
b = Integer.toHexString(bytes[i]);
// byte是两个字节的, 而上面的Integer.toHexString会把字节扩展为4个字节
buff.append(b.length() > 2 ? b.substring(6,8) : b);
buff.append(" ");
}
return buff.toString();
}
public static final String encodeBin (byte[] bytes)
{
StringBuffer buff =
new StringBuffer(bytes.length * 2);
String b;
for (int i=0; i<bytes.length ; i++)
{
b = Integer.toBinaryString(bytes[i]);
// byte是两个字节的, 而上面的Integer.toHexString会把字节扩展为4个字节
buff.append(b.substring(24,b.length()));
buff.append(" ");
}
return buff.toString();
}
在Java中,String.getBytes("GBK")获得的是gbk编码的字节串,打印出来之后得到的是对应编码的十进制值,转换成16进制后就和编码表中的值一样了。
最后可以根据输出的二进制编码,和unicode表对比。
示例:
str: 中国
gbk:
hex format is:d6 d0 b9 fa
utf8:
hex format is:e4 b8 ad e5 9b bd
Bin format is:11100100 10111000 10101101 11100101 10011011 10111101
convert back from utf8:e4 b8 ad e5 9b bd
其中,“中”的unicode编码是4e2d,二进制表示为0100 1110 0010 1101,从低位到高位按六位截取及补齐高位后就可以得到11100100 10111000 1010110 ,即为utf8编码。

发表评论