java里面byte数组和String字符串怎么转换

如题所述

1、string 转 byte[]

String str = "Hello";
byte[] srtbyte = str.getBytes();

2、byte[] 转 string

byte[] srtbyte;
String res = new String(srtbyte);
System.out.println(res);

3、设定编码方式相互转换

String str = "hello";
byte[] srtbyte = null;
try {
srtbyte = str.getBytes("UTF-8");
String res = new String(srtbyte,"UTF-8");
System.out.println(res);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2016-11-25
将数组内容转换成字符串:用的join方法
例如:数组arr={1,2,3,4};
arr1 = arr.join(",");
那么得到的arr1是一个字符串“1,2,3,4”

集合变数组用split(",")方法.表示将字符串按照逗号切割
相似回答