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();
}
本文介绍Java中byte数组与String之间的转换方法,并提供示例代码。文章首先展示如何将String转换为byte数组,接着演示如何从byte数组还原String。最后,还介绍了指定编码进行转换的过程。

1881

被折叠的 条评论
为什么被折叠?



