如何在 Java 中将大端字节缓冲区写入小端字节序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14496893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:27:55  来源:igfitidea点击:

How write big endian ByteBuffer to little endian in Java

javabytebufferendianness

提问by user1575243

I currently have a Java ByteBuffer that already has the data in Big Endian format. I then want to write to a binary file as Little Endian.

我目前有一个 Java ByteBuffer,它已经有 Big Endian 格式的数据。然后我想以 Little Endian 的身份写入二进制文件。

Here's the code which just writes the file still in Big Endian:

这是仅以 Big Endian 格式写入文件的代码:

 public void writeBinFile(String fileName, boolean append) throws FileNotFoundException, IOException
 {
     FileOutputStream outStream = null;
     try
     {
         outStream = new FileOutputStream(fileName, append);
         FileChannel out = outStream.getChannel();
         byteBuff.position(byteBuff.capacity());
         byteBuff.flip();
         byteBuff.order(ByteOrder.LITTLE_ENDIAN);
         out.write(byteBuff);
     }
     finally
     {
         if (outStream != null)
         {
            outStream.close();
         }
     }

 }

Note that byteBuff is a ByteBuffer that has been filled in Big Endian format.

请注意,byteBuff 是一个已填充为 Big Endian 格式的 ByteBuffer。

My last resort is a brute force method of creating another buffer and setting that ByteBuffer to little endian and then reading the "getInt" values from the original (big endian) buffer, and "setInt" the value to the little endian buffer. I'd imagine there is a better way...

我最后的手段是创建另一个缓冲区并将该 ByteBuffer 设置为小端,然后从原始(大端)缓冲区中读取“getInt”值,并将值“setInt”到小端缓冲区的蛮力方法。我想有更好的方法......

回答by Peter Lawrey

Endianess has no meaning for a byte[]. Endianess only matter for multi-byte data types like short, int, long, float, or double. The right time to get the endianess right is when you are writing the raw data to the bytes and reading the actual format.

字节序对于 byte[] 没有意义。Endianess 仅适用于多字节数据类型,如 short、int、long、float 或 double。获得正确字节顺序的正确时间是将原始数据写入字节并读取实际格式。

If you have a byte[] given to you, you must decode the original data types and re-encode them with the different endianness. I am sure you will agree this is a) not easy to do or ideal b) cannot be done automagically.

如果你有一个 byte[] 给你,你必须解码原始数据类型并用不同的字节序重新编码它们。我相信你会同意这是 a) 不容易做到或理想 b) 不能自动完成。

回答by Ogre Psalm33

Here is how I solved a similar problem, wanting to get the "endianness" of the Integers I'm writing to an output file correct:

这是我解决类似问题的方法,希望获得正确写入输出文件的整数的“字节序”:

byte[] theBytes = /* obtain a byte array that is the input */
ByteBuffer byteBuffer = ByteBuffer.wrap(theBytes);

ByteBuffer destByteBuffer = ByteBuffer.allocate(theBytes.length);
destByteBuffer.order(ByteOrder.LITTLE_ENDIAN);
IntBuffer destBuffer = destByteBuffer.asIntBuffer();

while (byteBuffer.hasRemaining())
{
    int element = byteBuffer.getInt();

    destBuffer.put(element);

    /* Could write destBuffer int-by-int here, or outside this loop */
}

There might be more efficient ways to do this, but for my particular problem, I had to apply a mathematical transformation to the elements as I copied them to the new buffer. But this should still work for your particular problem.

可能有更有效的方法来做到这一点,但对于我的特定问题,当我将它们复制到新缓冲区时,我必须对元素应用数学变换。但这应该仍然适用于您的特定问题。