字节与字符串相互转换

时间:2021-05-04 17:53:53
public class StringDemo {
    public static void main(String args[]){
        String str = "helloworld";
        byte data[] = str.getBytes(); //将字符串变为字节数组
        for(int x = 0;x < data.length; x++){
            data[x] -= 32;   //将helloworld变成大写
            System.out.print(data[x] + ",");
        }
        System.out.println(new String(data));
    }
}

String和byte相互转换

String s = "easonjim";//String变量 
byte b[] = s.getBytes();//String转换为byte[] 
String t = new String(b);//bytep[]转换为String,支持传递编码

 

 

输出

72,69,76,76,79,87,79,82,76,68,HELLOWORLD