Convert byte to int Type in Java – 在 Java 中将字节转换为 int 类型

最后修改: 2024年 1月 22日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Converting byte to int is a common operation, especially when dealing with low-level data manipulation, file I/O, or network communication. In this article, we’ll explore various methods through which we can achieve the byte to int conversion.

byte 转换为 int 是一种常见操作,尤其是在处理底层数据操作、文件 I/O 或网络通信时。在本文中,我们将探讨实现 byteint 转换的各种方法。

2. byte and int

2.byteint

In Java, byte and int are fundamental data types that serve distinct purposes in the representation of numerical values. A byte is an 8-bit signed data type with values ranging from -128 to 127. An int data type is a 32-bit signed integer, offering a wider range than byte, from -231 to 231-1 (-2,147,483,648 to 2,147,483,647).

在 Java 中,byteint 是基本数据类型,在表示数值时具有不同的用途。 byte 是一种 8 位带符号数据类型,其数值范围为 -128 至 127。 int数据类型是一种 32 位带符号整数,其数值范围比 byte 更广,从 -231 到 231-1 (-2,147,483,648 到 2,147,483,647)

3. Using Type Casting

3.使用类型铸造

One of the most straightforward and common approaches to performing the conversion is to simply typecast the byte variable to an int variable:

执行转换的一种最直接、最常见的方法是简单地将 typecast byte 变量转换为 int 变量:

class ByteToIntConversion {
    static int usingTypeCasting(byte b) {
        int i = b;
        return i;
    }
}

In this example, we’re directly converting a byte to an int variable through assignment. Let’s test it out:

在本例中,我们通过赋值将 byte 直接转换为 int 变量。让我们来测试一下:

@Test
void givenByte_whenUsingTypeCasting_thenConvertToInt() {
    byte b = -51;
    int result = ByteToIntConversion.usingTypeCasting(b);
    assertEquals(-51, result);
}

4. Using Integer.valueOf()

4.使用 Integer.valueOf()

The Integer class offers convenient methods for converting values from other primitive data types. We can employ its static method Integer.valueOf(), which facilitates the conversion of a byte to an int:

Integer 类为从其他 原始数据类型转换数值提供了方便的方法。我们可以使用它的静态方法 Integer.valueOf(),该方法有助于将 byte 转换为 int

static int usingIntegerValueOf(byte b){ 
    return Integer.valueOf(b);
}

The above code example takes a byte as an input and will return an Integer instance of the specified byte value. The Java Compiler will automatically apply the unboxing since the Integer class serves as a wrapper for the primitive data type int. We can perform a test to verify its expected behavior:

上面的代码示例将 byte 作为输入,并将返回指定 byte 值的 Integer 实例。由于 Integer 类是基元数据类型int的封装器,因此 Java 编译器将自动应用开箱。我们可以执行测试来验证其预期行为:

@Test 
void givenByte_whenUsingIntegerValueOf_thenConvertToInt() { 
    byte b = -51; 
    int result = ByteToIntConversion.usingIntegerValueOf(b); 

    assertEquals(-51, result); 
}

5. Using the Byte Class

5.使用 Byte

Byte class is a wrapper class for the primitive data type byte. It provides methods to work with byte values as objects, including conversion methods for handling byte values.

Byte class 是原始数据类型 byte 的封装类。它提供了将 byte 值作为对象处理的方法,包括处理 byte 值的转换方法。

5.1. Using intValue()

5.1.使用 intValue()

The Byte class offers an indirect approach to convert a byte to an int data type through its intValue() method. To make this method effective, we transform the primitive value to its object representation and then proceed with the conversion process:

Byte 类通过其 intValue() 方法提供了一种将 byte 转换为 int 数据类型的间接方法。为使该方法有效,我们先将基元值转换为对象表示,然后继续转换过程:

static int usingByteIntValue(byte b){
    Byte byteObj = new Byte(b);
    return byteObj.intValue();
}

In this example, the intValue() method returns an int value after performing a widening primitive conversion. Let’s test this out:

在本例中,intValue() 方法在执行加宽基元转换后返回一个 int 值。让我们来测试一下:

@Test
void givenByte_whenUsingByteIntValue_thenConvertToInt() { 
    byte b = -51; 
    int result = ByteToIntConversion.usingByteIntValue(b); 

    assertEquals(-51, result); 
}

5.2. Byte.toUnsignedInt()

5.2 Byte.toUnsignedInt().

Beginning with Java 8, the Byte class offers a utility method called toUnsignedInt for converting a byte to an unsigned integer. This method internally performs bitwise AND operation of the byte value with 0xff:

从 Java 8 开始,字节类提供了一个名为 toUnsignedInt 的实用程序方法,用于将 字节转换为 unsigned 整数。该方法在内部执行 bitwise0xff 的字节值的 AND 运算:

static int usingByteUnsignedInt(byte b){
    return Byte.toUnsignedInt(b);
}

It’s important to observe that by default, byte-to-int conversion retains the sign of the value. However, the above method treats the byte value as if it were an unsigned byte, producing the equivalent unsigned integer representation:

值得注意的是,默认情况下,字节到整数的转换会保留数值的符号。但是,上述方法会将 字节值当作无符号 字节处理,从而产生等效的无符号 整数表示:

@Test 
void givenByte_whenUsingByteUnsignedInt_thenConvertToInt() { 
    byte b = -51; 
    int result = ByteToIntConversion.usingByteUnsignedInt(b); 

    assertEquals(205, result); 
}

6. Conclusion

6.结论

In this tutorial, we’ve delved into different approaches for converting a byte to an int data type. Each approach provides a reliable way to perform the conversion. The choice depends on selecting the most suitable method for our specific use case.

在本教程中,我们深入探讨了将 byte 转换为 int 数据类型的不同方法。每种方法都提供了执行转换的可靠方式。选择取决于为我们的特定用例选择最合适的方法。

When working with negative numbers and aiming for their signed representation, we can consider using typecasting, Integer.valueOf(), or Byte class intValue() method. Alternatively, for unsigned conversion, we can opt for Byte.toUnsignedInt()  approach.

在处理负数时,我们可以考虑使用类型转换、Integer.valueOf()ByteintValue() 方法来实现有符号表示。另外,对于无符号转换,我们可以选择 Byte.toUnsignedInt() 方法。

As always, the full source code is available over on GitHub.

与往常一样,您可以在 GitHub 上获取完整的源代码