java里面byte数组和String字符串怎么转换,给个例子说明下他们之前详细转换z,
byte数组转换成String可以调用String的参数为byte数组的构造方法,代码如下:String res = new String(byte);
String转换成byte数组可以调用String的getByte方法,代码如下:byte[] srtbyte = str.getBytes();
温馨提示:答案为网友推荐,仅供参考
第1个回答 推荐于2017-09-18
1、string 转 byte[]
byte[] srtbyte = str.getBytes();
2、byte[] 转 string
String res = new String(srtbyte);
System.out.println(res);
3、设定编码方式相互转换
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个回答 2013-08-28
//string 转 byte[]
String str = "问题";
byte[] srtbyte = str.getBytes();
// byte[] 转 string
String res = new String(srtbyte);
System.out.println(res);
//当然还有可以设定编码方式的
String str = "问题";
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();
}本回答被网友采纳
String str = "问题";
byte[] srtbyte = str.getBytes();
// byte[] 转 string
String res = new String(srtbyte);
System.out.println(res);
//当然还有可以设定编码方式的
String str = "问题";
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();
}本回答被网友采纳