概述
编码:String->byte[]; str.getBytes(charsetName)
解码:byte[]->String; new String(byte[],charsetName);
编解码
示例1
package pack;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.util.Arrays;
public class Demo{
/**
* 用gbk编码 utf解码,然后再用utf编码 ,gbk解码,这样转不过来,因为gbk和utf-8里面都有中文,不过 iso8859-1可以
*/
public static void main(String[] args) throws Exception{
String s = "哈哈";
byte[] b = s.getBytes("gbk"); //gbk编码
System.out.println(Arrays.toString(b));
String str = new String(b,"utf-8"); //utf-8解码
System.out.println(str);
b = str.getBytes("utf-8");
System.out.println(Arrays.toString(b));
str = new String(b,"gbk");
System.out.println(str);
}
}
示例2
package pack;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.util.Arrays;
/**
* 把联通用gbk编码,然后把字节以二进制形式输出,是110开头,后面是10,所以系统就用utf-8去解码.
* 这个是特例,输出二进制时候,前面有很多个1,是32位的,输出低4位,就&上255
*/
public class Demo{
/**
* 用gbk编码 utf解码,然后再用utf编码 ,gbk解码,这样转不过来,因为gbk和utf-8里面都有中文,不过 iso8859-1可以
*/
public static void main(String[] args) throws Exception{
String s = "联通";
byte[] b = s.getBytes("gbk"); //gbk编码
for (byte b1 : b) {
System.out.println(Integer.toBinaryString(b1));
}
System.out.println(Arrays.toString(b));
byte[] bb = s.getBytes("utf-8"); //gbk编码
System.out.println(Arrays.toString(bb));
for(byte b2:bb){
System.out.println(Integer.toBinaryString(b2&255));
}
}
}