如何把字符转换为二进制,编码有许多,
但其中国际通用的有:
UTF-8
UTF-8 BOM
UTF-16
UTF-16 BOM
其中对于中文来说,中文的有GBK,gb2312
直接看下面例子把字符转换成二进制:
首先是GBK编码:
String str="大海啊,你都是水";
byte[] bs;
try{
bs=str.getBytes("gbk");
for (byte b : bs) {
System.out.println(b+",");
}
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
结果为:-76,-13,-70,-93,-80,-95,-93,-84,-60,
-29,-74,-68,-54,57,-53,-82,
接着是UTF-8:
String sta="蛤蟆啊,你四条腿";
byte[] ba;
try{
ba = sta.getBytes(); //默认utf-8
for (byte a : ba) {
System.out.println(a+",");
}
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
结果为:-24,-101,-92,-24,-97,-122,-27,-107,-118,-17,-68,-116,-28,-67,-96,-27,
-101,-101,-26,-99,-95,-24,-123,-65
有转过去自然有转回来,读取文件的时候经常会这样,因为文件读取的本身就是二进制,后面如果要读取文件的话,它本身就是二进制,二进制就要转换过来:
GBK转换:
byte[] bss = new byte[] { -76, -13, -70, -93, -80, -95,
-93, -84, -60,-29, -74, -68, -54, -57, -53, -82, };
String string = new String(bss, Charset.forName("GBK"));
System.out.println(string);
UTF-8转换:
byte[] bs7 = new byte[] { -24, -101, -92, -24, -97,
-122, -27, -107,-118, -17, -68, -116, -28, -67, -96,
-27, -101,-101, -26, -99,-95, -24, -123, -65 };
String str8 = new String(bs7, Charset.forName("UTF-8"));
System.out.println(str8);
结果: