Java Convert Bytes to Unsigned Bytes17 Mar 2025 | 3 min read 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 ByteThe following table depicts the conversion of byte to unsigned byte (int).
Conversion ProcessFirst, 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:
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). ![]() 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() MethodJava 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 |
Developers can insert a piece of code to run when the JVM is shutting down using a specific construct called a shutdown hook. It is useful when we need to do certain cleanup procedures in the event that the JVM is shutting down. When the VM is...
4 min read
In this section, we will create Java programs that generates binary numbers from the specified range (0 to n). The generation of binary numbers from 1 to n can be achieved through a binary tree. We know that in a tree, every node has two child nodes...
3 min read
Difference Between Java and Core Java Java is a programming language known for its simplicity, object-oriented nature, and platform independence. It consists of three major editions: Java Standard Edition (JSE), Java Enterprise Edition (JEE), and Java Micro Edition (JME). On the other hand, Core Java specifically...
5 min read
The sliding puzzle game is a classic and entertaining puzzle that has captivated people for generations. The objective of the game is to arrange the numbered tiles in the correct order by sliding them into the empty space. It seemingly simple task becomes increasingly challenging as...
8 min read
In Jackson, Tree Model Node is one of the most important concepts which we are going to discuss in this section. We will use Tree Model Node for various conversions and for adding, modifying, and removing nodes. Let's understand how we can create a Node, transform a...
32 min read
The most recent long-term-support (LTS) release is Java 12, the first "interim" release since Java 11, which was published on March 19, 2019. After the LTS version 11 of Java, Java 12 was released. The 6-month release cycle applies to JDK 12. On March 19, 2019,...
13 min read
ArrayList is similar to the array whose size can be modified. The ArrayList class is available in the Java.util package and extends the List interface. Adding and removing an element from the ArrayList is very easy by using its built-in methods add() and remove(). However, there...
4 min read
classes are used for connection-less socket programming using the UDP instead of TCP. Datagram Datagrams are collection of information sent from one device to another device via the established network. When the datagram is sent to the targeted device, there is no assurance that it will...
4 min read
In a binary tree, display the nodes of the odd level in any order. Consider the root node present at level 1. For the following binary tree: The odd level nodes are: 20 25 3 5 7. As we have to display the nodes in any order. Therefore, 20 25 5...
4 min read
The List is one of the widely used collection interfaces that is used to store the ordered collection. The List interface maintains the insertion order of elements and can store duplicate values also. In this section, we will understand how we can convert a List into an...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India