package demo.io;
import java.io.UnsupportedEncodingException;
class 字符串的编码和解码 {
public static void main(String[] args) throws UnsupportedEncodingException {
String str = "你好";
byte[] buf = str.getBytes("GBK");//可能抛出编码格式错误异常
System.out.println("\"你好\"对应的GBK编码:");
for (byte b : buf) {
System.out.print(b + " ");
}
System.out.println();
System.out.println("解码GBK:" + new String(buf, "GBK"));
System.out.println("----------------------------------------------------------");
buf = str.getBytes();//编译器默认是UTF-8,而doc默认GBK
System.out.println("\"你好\"对应的UTF-8编码:");
for (byte b : buf) {
System.out.print(b + " ");
}
System.out.println();
System.out.println("解码UTF-8:" + new String(buf/*, "iso8859-1"*/));//错误解码 拉丁编码
}
}
字符串的编码和解码
最新推荐文章于 2023-09-05 15:21:57 发布