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

2024-12-24 22:28:12
推荐回答(2个)
回答1:

  1. string转byte数组:byte[] midbytes=isoString.getBytes("UTF8");

  2. byte数组转string:String isoString = new String(bytes,"ISO-8859-1");

具体你自己想要什么编码,里面可以换参数

回答2:

//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();
}