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

如题所述

第1个回答  2014-11-18
//string 转 byte[]
String str = "Hello";
byte[] srtbyte = str.getBytes();
// byte[] 转 string
String res = new String(srtbyte);
System.out.println(res);

//当然还有可以设定编码方式

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个回答  2014-11-18
String s1 = "abc";
//String转byte数组
byte[] b = s1.getBytes();
//byte数组转String
String s2 = new String(b);

第3个回答  2014-11-18
String类有个getBytes方法, 这个方法有很多重载的方法, String类还有new String(byte[] , String)这样的方法, 也有很多个重载的
第4个回答  2014-11-18
byte toString 然后拼接、。。。。。
第5个回答  2014-11-18
相似回答