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

En Java, convertir un byte en su equivalente hexadecimal

Para convertir el byte en su equivalente hexadecimal, use el método toHexString() de Java.

Primero, tomemos un valor de byte.

byte val1 = (byte)90;

Antes de usar este método, haremos más operaciones. Ahora ocultamos el valor del byte:

int res = val1 & 0xFF;

Ahora veamos el ejemplo completo y usemos el método toHexString() para convertir el byte en su equivalente hexadecimal.

Ejemplo

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

Resultado de salida

Byte = 90
Hexadecimal = 5a