Java基础 Java中编码、解码的方法

所谓编码,就是把我们要存储的数据变成能真正的存储在硬盘当中的字节数据

 

编码的方法:

public  byte[]  getBytes ()   →   使用默认方式进行编码(idea默认使用UTF-8;eclipse默认使用GBK)

public  byte[]  getBytes (String  charsetName)   →   使用指定方式进行编码

 

解码的方法:

String (byte[]  bytes)   →   使用默认方式进行解码

String (byte[]  bytes ,  String  charsetName)   →   使用指定方式进行解码

 

 

eg:编码

String str = "ai你吆";
byte[] bytes = str.getBytes("UTF-8");
System.out.println(Arrays.toString(bytes));
//打印结果:[97, 105, -28, -67, -96, -27, -112, -122]

 

解码

byte[] bytes = {97, 105, -28, -67, -96, -27, -112, -122};
String str = new String(bytes, "utf-8");
System.out.println(str);
//打印结果:ai你吆

 

posted @ 2023-10-23 20:03  1stzz1  阅读(154)  评论(0)    收藏  举报