Java Program to convert an UNSIGNED byte to a JAVA type


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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 26-Jun-2020

140 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements