19 Aug How to convert byte to string in Java
Let’s say the following is our byte value and we want to convert it to string:
byte b = 8;
We will use the valueOf() method to convert byte to string. The syntax of the method is as follows:
String.valueOf(byte b);
Above, b is the byte value to be converted.
Example 1
Using the valueOf() method, convert byte to string as shown below:
public class Demo {
public static void main(String[] args) {
byte b = 5;
System.out.println("Byte = "+b);
System.out.println("String = "+String.valueOf(b));
}
}
Output
Byte = 5 String = 5
We can also use the toString() method to convert byte to string. The syntax of the method is as follows:
Byte.toString(byte b);
Above, b is the byte value to be converted.
Example 2
public class Demo {
public static void main(String[] args) {
byte b = 10;
System.out.println("Byte = "+b);
System.out.println("String = "+Byte.toString(b));
}
}
Output
Byte = 10 String = 10
No Comments