天天看点

Java中 byte[] 数组与 String 字符串的转化

1. String 转 byte[]

String str = "Hello";//声明一个字符串
byte[] srtbyte = str.getBytes();//使用string类的getBytes方法进行转换
           

2. byte[] 转 String

srtbyte = str.getBytes("UTF-8");//设定转换的编码格式
String res = new String(srtbyte,"UTF-8");