Java Data Type How to - Get the value stored in a byte as an unsigned integer








Question

We would like to know how to get the value stored in a byte as an unsigned integer.

Answer

public class Main {
  public static void main(String[] args) {
    byte b = -10;
    int x = Byte.toUnsignedInt(b);
    System.out.println("Signed value in byte   = " + b);
    System.out.println("Unsigned value in  byte   = " + x);

  }
}

The code above generates the following result.