Javatpoint Logo
Javatpoint Logo

Java Convert Bytes to Unsigned Bytes

In Java, byte is data type. It is 8-bit signed (+ ive or - ive) values from -128 to 127. The range of unsigned byte is 0 to 255. Note that Java does not provide unsigned byte. If we need to represent a number as unsigned byte, we must cast byte to int and mask (&) the new int with a &0xff. It gives the last 8-bits or prevents sign extension.

Example:

byte b = -1;

int num = b &0xff //converts byte to unsigned byte in an integer

Why we need casting (byte to int)?

When we need to represent signed numbers in Java, we find 2's complement. In 2's complements the left most bit represent the sign (+ ive or - ive). The bit 0 denotes positive and 1 denotes negative. The rest of the bits denotes the value from -128 to 127. Therefore, it is called 8-bit byte only have 7 bits to store the values. other extra values range from 128 to 255 are unable to fit into a single byte. So, we can cast it to 32-bit unsigned integer for more spaces (bits).

Byte to Unsigned Byte

The following table depicts the conversion of byte to unsigned byte (int).

Byte Unsigned Byte
1 1
2 2
127 127
-128 128
-127 129
-126 130
-2 254
-1 255

Conversion Process

First, we cast the byte 8-bit into 32-bit. For example, convert -1.

Find binary signed 2's complement of -1 by using the following steps:

  • Write binary of 1, i.e. 0000 0001
  • Invert bits of the binary, i.e. 1111 1110.
  • Add 1 to the inverted bits, i.e. 1111 1111

Hence, we get the binary of -1 i.e. 1111 1111. Where leftmost bit 1 represent negative sign while 0 represent the positive sign.

Note that when we convert or cast a byte to an int, it increases the bits from 8 to 32 bit. The sign extension will apply and fill in the values of the increased bits.

8-bit representation 1111 1111 (-1)

Converting byte into int

32-bit representation sign extension

Now we will get the last 8-bits by performing the &x0ff (bitwise AND).

Java Convert Bytes to Unsigned Bytes

At last, convert binary to decimal.

1111 1111

128+64+32+16+8+4+2+1=255

Therefore -1 is 255.

Using Java 8 Byte.toUnsignedInt() Method

Java 8 provides the built-in method toUnsignedInt() that is defined in the Byte class. It supports unsigned operations. The method converts a signed byte into an unsigned integer.

In an unsigned conversion, the high-order 24 bits of the int are zero and the low-order 8 bits are equal to the bits of the byte argument.

Consequently, zero and positive byte values are mapped to a numerically equal int value and negative byte values are mapped to an int value equal to the input plus 28.

Syntax:

The method accepts a value to convert to an unsigned int. It returns the argument converted to int by an unsigned conversion.

BytetoUnsignedByte1.java

Output:

-8
248

Let's see another approach to convert byte to an unsigned integer.

BytetoUnsignedByte2.java

Output:

244

The above program can also be written as follows.

BytetoUnsignedByte3.java

Output:

Given Number: -5
251






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA