Convert a byte to hexadecimal equivalent in Java

Java 8Object Oriented ProgrammingProgramming

To convert a byte to hexadecimal equivalent, use the toHexString() method in Java.

Firstly, let us take a byte value.

byte val1 = (byte)90;

Before using the method, let us do some more manipulations. Mask the byte value now:

int res = val1 & 0xFF;

Let us now see the complete example and use the toHexString() method to convert a byte to hexadecimal equivalent.

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      byte val1 = (byte)90;
      System.out.println("Byte = "+val1);
      int res = val1 & 0xFF;
      System.out.println("Hexadecimal = "+Integer.toHexString(res));
   }
}

Output

Byte = 90
Hexadecimal = 5a
raja
Published on 12-Nov-2018 12:04:27
Advertisements