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


A Byte class is a subclass of Number class and it can wrap a value of primitive type byte in an object. An object of type Byte contains a single field whose type is a byte. The important methods of Byte class are byteValue(), compare(), compareTo(), decode(), parseByte(), valueOf() and etc. We can convert a hexadecimal value to a byte using the method decode().byteValue() of Byte class.

Syntax

public final class Byte extends Number implements Comparable<Byte>

Example

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

Output

10

Updated on: 22-Nov-2023

106 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements