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
raja
Published on 01-Aug-2019 16:20:08
Advertisements