Java Program to convert an UNSIGNED byte to a JAVA type

Java 8Object Oriented ProgrammingProgramming

Firstly, let us declare byte values.

byte val1 = 127;
byte val2 = -128;

To convert the above given unsigned byte, you can use the following. Here, we are first implementing it for the variable “val1”.

(int) val1 & 0xFF

Now for the second variable “val2”.

(int) val2 & 0xFF

Let us see the complete example to convert an UNSIGNED bye to a JAVA type.

Example

 Live Demo

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      byte val1 = 127;
      byte val2 = -128;
      System.out.println(val1);
      System.out.println((int) val1 & 0xFF);
      System.out.println(val2);
      System.out.println((int) val2 & 0xFF);
   }
}

Output

127
127
-128
128
raja
Published on 12-Nov-2018 10:51:46
Advertisements