java.lang.Byte toUnsignedInt(byte x)

Description

The toUnsignedInt(byte x) method of Byte class Converts the argument to an int by an unsigned conversion. In an unsigned conversion to an int, 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.

Method Syntax

public static int toUnsignedInt(byte x)

Method Argument

Data Type Parameter Description
byte x the value to convert to an unsigned int

Method Returns

The toUnsignedInt(byte x) method of Byte class returns the argument converted to int by an unsigned conversion.

Compatibility

Requires Java 1.8 and up

Java Byte toUnsignedInt(byte x) Example

Below is a simple java example on the usage of toUnsignedInt(byte x) method of Byte class.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

/*
 * This example source code demonstrates the use of 
 * toUnsignedInt(byte x) method of Byte class.
 */

public class ByteToUnsignedIntExample {

	public static void main(String[] args) {
		
		// ask for user input
		System.out.print("Enter a number:");

		// read the user input
		Scanner s = new Scanner(System.in);
		String value = s.nextLine();
		s.close();
		
		Byte byteValue = new Byte(value);
		// convert byteValue into unsign int
		int result = Byte.toUnsignedInt(byteValue);	
		
		// print the result
		System.out.println("unsigned int value is "+result);

		
	}

}

Basically on the above example, we have asked the user to enter a number on the console. Then we printed the result of the toUnsignedInt method. As you would have noticed positive numbers would result to the same number but for negative numbers that’s where we would see the difference in result.

Sample Output

Below is the sample output when you run the above example.

java Byte toUnsignedInt() example output