Java Convert Hexadecimal String to Byte

Tags: Hexadecimal byte

In this Java core tutorial we learn how to convert a hexadecimal String into a byte value in Java programming language.

How to convert hexadecimal to byte in Java

In Java, with a given String in hexadecimal number format we can use the Byte.decode(String nm) static method to convert it to a byte value as the example Java code below.

ConvertHexToByteExample.java

public class ConvertHexToByteExample {
    public static void main(String... args) {
        String hexValue1 = "0xf"; // String start with 0x
        String hexValue2 = "0Xe"; // String start with 0X
        String hexValue3 = "#30"; // String start with #

        // Convert Hexadecimal String to Byte
        byte byteValue1 = Byte.decode(hexValue1);
        byte byteValue2 = Byte.decode(hexValue2);
        byte byteValue3 = Byte.decode(hexValue3);

        System.out.println("hexValue1: " + hexValue1);
        System.out.println("byteValue1: " + byteValue1);
        System.out.println("\nhexValue2: " + hexValue2);
        System.out.println("byteValue2: " + byteValue2);
        System.out.println("\nhexValue3: " + hexValue3);
        System.out.println("byteValue3: " + byteValue3);
    }
}
The output as below.
hexValue1: 0xf
byteValue1: 15

hexValue2: 0Xe
byteValue2: 14

hexValue3: #30
byteValue3: 48

Happy Coding 😊

Java Convert Byte to Hexadecimal String

Java Convert Byte to Octal String

Java Convert Octal String to Byte

Java Convert Byte to BigDecimal

Java Convert Byte to BigInteger