Convert String to byte
In this chapter you will learn:
- 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
Decode a string to a byte
decode()
method from Byte class accepts decimal, hexadecimal,
and octal numbers given by the following grammar:
(+/-)0x for HexDigits
(+/-)0X for HexDigits
(+/-)# for HexDigits
(+/-)0 for OctalDigits
The following code use decode
method from Byte class to do the decoding for strings
which represents the byte
values
public class Main {
public static void main(String[] args) {
//from j a v a2 s.c o m
System.out.println("Decimal 10:"+Byte.decode("10"));
System.out.println("Octal 10:"+Byte.decode("010"));
System.out.println("Hex F:"+Byte.decode("0XF"));
System.out.println("Negative Hex F:"+Byte.decode("-0XF"));
}
}
The output:
Convert string value to byte value
The following table lists the methods defined in Byte
class which we can use to convert
string value to byte value.
Method | Description |
---|---|
static Byte valueOf(String s) | Returns a Byte object holding the value given by the specified String. |
static Byte valueOf(String s, int radix) | Returns a Byte object holding the value extracted from the specified String when parsed with the radix given by the second argument. |
static byte parseByte(String s) | Parses the string argument as a signed decimal byte. |
The following code uses Byte.parseByte
to convert string value to byte value.
public class Main {
public static void main(String[] args) {
System.out.println("parse string to byte:"+Byte.parseByte("10"));
}
}
The output:
Convert to byte with radix
Byte.parseByte(String s, int radix)
parses the string argument as a
signed byte in the radix specified by the second argument and returns a byte
value.
In the following code we convert string 10 based on radix 8.
public class Main {
public static void main(String[] args) {
/*from j a v a 2 s .c o m*/
System.out.println("parse string to byte:"+Byte.parseByte("10", 8));
}
}
The output:
And we use the valueOf methods in the same way:
public class Main {
public static void main(String[] args) {
/*j a v a 2 s . c om*/
System.out.println("parse string to byte:"+Byte.valueOf("10"));
System.out.println("parse string to byte:"+Byte.valueOf("10", 8));
}
}
The output:
Value of range
The following code converts the first String value '65' to byte with no problem.
When converting "129" to byte java.lang.NumberFormatException
is thrown since the 129 is out of the range of a byte value.
byte
is a signed 8-bit type that has a range from -128 to 127.
public class Main {
/* j a va 2s . c o m*/
public static void main(String[] args) {
String s = "65";
byte b = Byte.valueOf(s);
System.out.println(b);
// Causes a NumberFormatException since the value is out of range
System.out.println(Byte.valueOf("129"));
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter: