Convert Byte to String
In this chapter you will learn:
Convert byte value to string value
There are two methods we can use to convert a byte value to a string value.
First method is the toString method from Byte class.
String toString()
returns a String object representing this Byte's value.
Another way is to use the static method toString(byte b).
static String toString(byte b)
returns a new String object representing the specified byte.
The following two examples is showing how to convert byte value to string value.
public class Main {
public static void main(String[] args) {
Byte byteObject = new Byte("10");
//from j a v a2 s. c o m
String str = byteObject.toString();
System.out.println("str:"+str);
}
}
The outputs:
The code below calls the static method to do the conversion.
public class Main {
public static void main(String[] args) {
//from ja v a 2 s .c o m
byte b = 10;
System.out.println("str:"+Byte.toString(b));
}
}
The output:
We can also convert byte to String by concatenating byte value with an empty String.
public class Main {
public static void main(String[] args) {
byte b = 65;
//from ja v a2 s . c om
System.out.println(b + "");
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- How to decode a string to a byte
- How to convert string value to byte value
- How to convert string to byte with radix
- java.lang.NumberFormatException when byte value is out of range
Home » Java Tutorial » Primitive Data Types