java里面byte数组和String字符串怎么转换,给个例子说明下他们之前详细转换z,
推荐于2016-02-17 12:16:43
最佳答案
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();
}