English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Convert byte to equivalent hexadecimal in Java

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

First, let's take a byte value.

byte val1 = (byte)90;

Before using this method, let's do more operations. Now mask the byte value:

int res = val1 & 0xFF;

Now let's see the complete example and use the toHexString() method to convert the byte to its equivalent hexadecimal.

Example

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 Result

Byte = 90
Hexadecimal = 5a