How can we convert a hexadecimal value to a byte in Java?



This article will discuss how to convert a hexadecimal value to a byte value in Java. Before converting it to a byte, let's first understand what hexadecimal and byte values are.

Hexadecimal Value

In Java, a hexadecimal number is a base-16 number represented using the digits 0-9 and the letters A-F or a-f, where A-F represent the decimal values 10-15 respectively.

The hexadecimal values are:

  • 0x1A
  • 0xFF
  • 0x0A

Byte Value

In Java, a byte is a data type that can store whole numbers between the range of -128 to 127. It uses 8 bits of memory. For example, 0, 1, -1, 2, 3, -6, etc, all are valid byte values.

The important methods of the Byte class are -

  • byteValue()
  • compare()
  • compareTo()
  • decode()
  • parseByte()
  • valueOf()

To understand how the hexadecimal value is converted to a byte, let's take an input and output scenario:

Input: 0x0A

Output: 10

In 0x0A: A represents 10 in decimal, so 0x0A = (0x16)+10 = 10. Hence, the byte value is 10.

Converting Hexadecimal to Byte using decode().byteValue() Method

In Java, we can convert a hexadecimal value to a byte using the decode().byteValue method of the Byte class. The decode() method accepts a hexadecimal string as a parameter, and byteValue() converts it to a byte value.

Byte.decode("hex_value").byteValue();

Here, the hex_value is a hexadecimal value that needs to be converted to a byte value.

Example

The following example converts the given hexadecimal value "0x0A" to a byte value -

public class ConvertHexaDecimalToByte {
   public static void main(String args[]) {
      String hex_value = "0x0a";
      System.out.println("Hexadecimal value is: " + hex_value);
      byte b = Byte.decode(hex_value).byteValue(); // convert hexadecimal value to byte.
      System.out.println("Byte value of " + hex_value + " is: " + b);
   }
}

Output

The above program produces the following output -

Hexadecimal value is: 0x0a
Byte value of 0x0a is: 10

Using Integer.parseInt() and casting to byte

This is another way to convert a given hexadecimal value to a byte value. The Integer.parseInt() method is used to convert a string representation of a number in a specified base (radix) to an int.

Syntax

Following is the syntax of the Integer.parseInt() method -

Integer.parseInt(String s, int radix);

Here,

  • s is the string containing the number you want to convert.
  • radix is the base of the number system.

Example

In the example below, we use Integer.parseInt() to convert the string representation of a number in a specified "radix", and then use "typecasting" to convert the "parsed number" to a "byte" -

public class ConvertHexaDecimalToByte {
   public static void main(String args[]) {
      String hex = "1A";
      System.out.println("Hexadecimal value is: " + hex);
      int decimalValue = Integer.parseInt(hex, 16);
      byte b = (byte) decimalValue;
      System.out.println("Byte value of " + hex + " is: " + b);
   }
}

Output

Following is the output of the above program -

Hexadecimal value is: 1A
Byte value of 1A is: 26
Updated on: 2025-05-29T19:03:15+05:30

232 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements