Convert a Float to a Byte Array in Java – 在Java中把浮点数转换为字节数组

最后修改: 2019年 1月 24日

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

1. Overview

1.概述

In this quick tutorial, we’ll explore a few examples of using Java to convert a float to a byte array and vice versa.

在这个快速教程中,我们将探讨几个使用Java将浮点数转换成字节数组的例子,反之亦然。

This is simple if we convert an int or a long to a byte array as Java Bitwise Operators works only on integer types. However, for a float, we need to use another layer of conversion.

如果我们将int或long转换为字节数组,这很简单,因为Java Bitwise Operators只对整数类型起作用。然而,对于浮点数,我们需要使用另一层的转换。

For instance, we can use APIs provided by the Float class or ByteBuffer class of java.nio package.

例如,我们可以使用Float类或java.nio包的ByteBuffer类提供的API。

2. Float to Byte Array Conversion

2.浮点数到字节数组的转换

As we know, the size of a float in Java is 32 bit which is similar to an int. So we can use floatToIntBits or floatToRawIntBits functions available in the Float class of Java. And then shift the bits to return a byte array. Click here to learn more about bit shifting operations.

我们知道,Java中浮点数的大小是32位,与int相似。所以我们可以使用Java的floatToIntBits或floatToRawIntBits类中的函数。然后移位,返回一个字节数组。点击这里,了解更多关于比特移位的操作。

The difference between both is floatToRawIntBits preserves Not-a-Number (NaN) values as well. Here shifting the bits has been done through a technique called Narrowing Primitive Conversion.

两者的区别在于floatToRawIntBits也保留了非数字(NaN)值。在这里,通过一种叫做Narrowing Primitive Conversion的技术完成了位的移动。

Firstly let’s have a look of the code with Float class function:

首先让我们看一下带有Float类函数的代码。

public static byte[] floatToByteArray(float value) {
    int intBits =  Float.floatToIntBits(value);
    return new byte[] {
      (byte) (intBits >> 24), (byte) (intBits >> 16), (byte) (intBits >> 8), (byte) (intBits) };
}

Secondly a neat way of conversion using ByteBuffer:

其次是使用ByteBuffer的一种整齐的转换方式。

ByteBuffer.allocate(4).putFloat(value).array();

3. Byte Array to Float Conversion

3.字节数组到浮点数的转换

Let’s now convert a byte array into a float using Float class function intBitsToFloat.

现在让我们使用Float类函数intBitsToFloat将一个字节数组转换为浮点数。

However, we need to first convert a byte array into int bits using the left shift:

然而,我们需要首先使用左移将字节数组转换为int位。

public static float byteArrayToFloat(byte[] bytes) {
    int intBits = 
      bytes[0] << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | (bytes[3] & 0xFF);
    return Float.intBitsToFloat(intBits);  
}

Converting a byte array into a float using ByteBuffer is as simple as this:

使用ByteBuffer将一个字节数组转换为浮点数,就像这样简单。

ByteBuffer.wrap(bytes).getFloat();

4. Unit Testing

4.单元测试

Let’s look at simple unit test cases for implementation:

让我们来看看简单的单元测试案例的实现。

public void givenAFloat_thenConvertToByteArray() {
    assertArrayEquals(new byte[] { 63, -116, -52, -51}, floatToByteArray(1.1f));
}

@Test
public void givenAByteArray_thenConvertToFloat() {
   assertEquals(1.1f, byteArrayToFloat(new byte[] { 63, -116, -52, -51}), 0);
}

5. Conclusion

5.总结

We have seen different ways of float to byte conversion and vice-versa.

我们已经看到了不同的浮点数到字节数的转换方式,反之亦然。

Float class provides functions as a workaround for such conversion. However, ByteBuffer provides a neat way to do this. For this reason, I suggest using it wherever possible.

Float类提供了函数作为这种转换的变通方法。然而,ByteBuffer提供了一个整洁的方法来做这个。出于这个原因,我建议尽可能地使用它。

The complete source code of these implementations and unit test cases can be found in the GitHub project.

这些实现的完整源代码和单元测试案例可以在GitHub项目中找到。