How to convert an UNSIGNED byte to Java type

An UNSIGNED byte can be converted to a Java type. Let us first learn about the signed and unsigned byte values:

UNSIGNED byte: 0 to 255
SIGNED byte: -128 to 127

Following is an example to convert an UNSIGNED byte to Java type:

Example 1

public class Example {

  public static void main(String[] args) {
   
    byte a = -1;

    System.out.print("UNSIGNED converted to Java type: ");
    System.out.println((int) a & 0xFF);  
  }
}

Output

UNSIGNED converted to Java type: 255

Example 2

public class Example {

  public static void main(String[] args) {
   
    byte a = -5;

    System.out.print("UNSIGNED converted to Java type: ");
    System.out.println((int) a & 0xFF);
   }
}

Output

UNSIGNED converted to Java type: 251

Example 3

public class Example {

  public static void main(String[] args) {
   
    byte a = -126;

    System.out.print("UNSIGNED converted to Java type: ");
    System.out.println((int) a & 0xFF);  
  }
}

Output

UNSIGNED converted to Java type: 130
How to Compare two Byte Arrays in Java
Java Program to convert a byte to hexadecimal equivalent
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment