Java Program to get the value stored in a byte as an unsigned integer

Java 8Object Oriented ProgrammingProgramming

Let us first create signed byte −

byte signedVal = -100;

Now convert a byte to an unsigned integer −

int unsignedVal = Byte.toUnsignedInt(signedVal);

Example

public class Demo {
   public static void main(String[] args) {
      byte signedVal = -100;
      int unsignedVal = Byte.toUnsignedInt(signedVal);
      System.out.println("Signed value (byte) = " + signedVal);
      System.out.println("Unsigned value (byte) = " + unsignedVal);
   }
}

Output

Signed value (byte) = -100
Unsigned value (byte) = 156
raja
Published on 26-Apr-2019 06:25:23
Advertisements