Java Program to convert a byte to hexadecimal equivalent

The toHexString() method in Java converts a byte to hexadecimal equivalent. Following are some examples with output:

Example 1

public class Demo {

  public static void main(String[] args) {
   
    byte b = 75;
    int i = b & 0xFF;

    System.out.println("Hexadecimal equivalent of byte = "+Integer.toHexString(i));
  }
}

Output

Hexadecimal equivalent of byte = 4b

Example 2

public class Demo {

  public static void main(String[] args) {
   
    byte b = 125;

    int i = b & 0xFF;
    System.out.println("Hexadecimal equivalent of byte = "+Integer.toHexString(i));
  }
}

Output

Hexadecimal equivalent of byte = 7d
How to convert an UNSIGNED byte to Java type
Integer.lowestOneBit() method in Java
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment