在Java中,有多种方法可以将字节转换为字符串。
使用toString()方法,您可以轻松地将字节转换为字符串,如下所示-
public class Demo {
public static void main(String[] args) {
byte res = 87;
//字节到字符串
System.out.println(Byte.toString(res));
}
}输出结果
87
在上面的示例中,我们采用了一个字节值。
byte res = 87;
这样,要转换为字符串,使用方法toString()如下所示-
Byte.toString(res);
让我们看另一个将字节转换为字符串的示例。
public class Demo {
public static void main(String[] args) {
byte val = 77;
System.out.println(new String(new byte[] {val}));
}
}输出结果
M