java byte 数组里存的是ascii码,怎么转成字符串

2025-03-26 20:58:39
推荐回答(4个)
回答1:

byte[] b=new byte[]{65,66,67,68};//字节数组
        String s=new String(b,"ascii");//第二个参数指定编码方式
        System.out.print(s);

回答2:

new String(byteArray);
String(byte[] bytes)
Constructs a new String by decoding the specified array of bytes using the platform's default charset.

换成这个构造函数:

String(byte[] bytes, String charsetName)
Constructs a new String by decoding the specified array of bytes using the specified charset.

回答3:

如果有中文的话,改成这个:new String(aa, "GB2312");告诉它转成GB2312格式的内容。

回答4:

str=new String(aa,,"ISO-8859-1");