English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java program converts UNSIGNED byte to JAVA type

First, let's declare the byte value.

byte val1 = 127;
byte val2 = -128;

To convert the unsigned byte given above, you can use the following. Here, we first declare the variable "val1" implement it.

(int) val1 & 0xFF

Now it's the second variable "val2"

(int) val2 & 0xFF

Let's see a complete example, converting UNSIGNED byte to JAVA type.

Example

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 Result

127
127
-128
128