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:
1 2 3 |
byte b = 8; |
We will use the valueOf() method to convert byte to string. The syntax of the method is as follows:
1 2 3 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 |
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
1 2 3 4 |
Byte = 5 String = 5 |
We can also use the toString() method to convert byte to string. The syntax of the method is as follows:
1 2 3 |
Byte.toString(byte b); |
Above, b is the byte value to be converted.
Example 2
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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
1 2 3 4 |
Byte = 10 String = 10 |
No Comments