1、将字符转换成byte数组
String str = "罗长";
byte[] sb = str.getBytes();
2、将byte数组转换成字符
byte[] b={(byte)0xB8,(byte)0xDF,(byte)0xCB,(byte)0xD9};
String str= new String (b);
一、字符串转字节数组
String str = “金灿灿”;
byte[] bytes = str.getBytes();
如果转换的时候不设置编码格式,默认是以环境的字符集编码。
1.utf-8 : byte[] bytes = str.getBytes(“utf-8”);//中文是占三个字节
2.gbk : byte[] bytes = str.getBytes(“gbk”);//中文是占两个字节
二、字节数组转字符串
1.utf-8 : String s = new String(bytes ,“utf-8”);
2.gbk : String s = new String(bytes ,“gbk”);