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.


 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));


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