Convert a byte to hexadecimal equivalent in Java


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

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 26-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements