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-03-08
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();
}
第2个回答  推荐于2017-11-22

//string 转 byte[]

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

// byte[] 转 string

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


求采纳

本回答被提问者采纳
第3个回答  2020-01-14
byte数组转换成String可以调bai用String的参数为dubyte数组的构zhi造方法dao,代码如下:内String
res
=
new
String(byte);
String转换成byte数组可以容调用String的getByte方法,代码如下:byte[]
srtbyte
=
str.getBytes();
相似回答