java 里面的string 和byte[] 怎么互转

时间:2021-10-23 11:51:05

public class TestBytes {

    private static String FORMAT = "UTF-8";
public static void main(String[] args) throws UnsupportedEncodingException {
byte[] bytes = new byte[] { 50, 0, -1, 28, -24 };
String sendString=new String( bytes ,FORMAT);
byte[] sendBytes= sendString.getBytes(FORMAT); String recString=new String(sendBytes ,FORMAT);
byte[] mybytes=recString.getBytes(FORMAT); for(int i = 0 ; i < mybytes.length ; i++){
System.out.print(" ");
System.out.print(mybytes[i ]);
}
} }

输出结果是: 50 0 -17 -65 -67 28 -17 -65 -67,为什么会不一样!?

FORMAT = "ISO-8859-1" 就可以了!