Convert Byte to Int in Java

  1. Convert Byte to Int Using the Byte Wrapper Class and Casting in Java
  2. Using Byte to the Unsigned Byte Conversion

A byte data type is defined as an 8-bit signed two’s complement integer, whereas an int data type can be classified as a 32-bit signed two’s complement integer. Unless there is concern about memory, an integer is the default data type used to store integral values. In this article, we’re going to discuss how you can convert a byte into an int data type.

Convert Byte to Int Using the Byte Wrapper Class and Casting in Java

A byte holds 0 as the default value and its range varies from -128 = (-2^7) to 127 = (2^7 -1) . An integer holds a default value of 0, and its range varies from -2^31 to 2^31-1. The Wrapper class for byte is Byte, whereas for int is Integer.

If the byte value exceeds their range, the compiler automatically promotes it to an int variable. In Java, there is only a signed integer type (no unsigned concept); this means that all the integer primitive types, including the byte, are signed. However, the range is 0 to 255 for an unsigned byte data type.

The whole point of byte-to-int conversion is to preserve the sign of the value. Here in the code, we have a byte type variable b, which we’ll convert into an int type variable i. Hence, it’s not downcast. We can directly assign the byte to the int data type.

Secondly, we have a Wrapper class method intValue() that returns the value of byte as an int after widening the primitive conversion as we’re storing a smaller data type into a larger one.

If we take the byte as unsigned, then we have the Byte.toUnsignedInt() method that converts the argument passed to an int using the unsigned conversion.

public class JavaByteToInt {
    public static void main(String args[]){
        byte b = -98;
        System.out.println("byte b : = "+b);
        int i = b;
        System.out.println("int i : "+i);

        Byte byt  = new Byte(b);
        int i1 = byt.intValue();
        int i2 = Byte.toUnsignedInt(byt);
        System.out.println("int i1 : "+i1);
        System.out.println("int i2 : "+i2);

    }
}

Output:

byte b : = -98
int i : -98
int i1 : -98
int i2 : 158

Using Byte to the Unsigned Byte Conversion

The range for an unsigned byte data type goes from 0 to 255; however, Java doesn’t have unsigned bytes. What you can do is cast the byte into an int to make an unsigned byte, and mask (bitwise) the new int with a 0xff to get the last 8 bits or prevent the sign extension.

When a byte value is used with the & operator, it automatically casts the byte to an integer. The Hex value of 0x000000FF is denoted as 0xFF.

public class JavaByteToInt {
    public static void main(String args[]){
        byte b = -127;
        int i = (b & 0xFF);
        System.out.println(i);

    }
}

Output:

129
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Java Byte

  • Convert Java String Into Byte
  • Related Article - Java Int

  • Convert an Integer to a String in Java
  • Write Byte to File in Java