Java中字符串和byte数组互相转换

1.string 字符串转 byte[]数组


String str = "Hello";

byte[] srtbyte = str.getBytes();

2.. byte[] 转 string

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

    }

你可能感兴趣的:(新知识的学习)