【问题标题】:convert little Endian file into big Endian将小端文件转换为大端文件
【发布时间】:2010-08-09 08:30:48
【问题描述】:

我怎样才能将一个小的 Endian 二进制文件转换成大的 Endian 二进制文件。我有一个用 C 编写的二进制二进制文件,我正在使用 Java 中的 DataInputStream 读取这个文件,该文件以大端格式读取。我还查看了 ByteBuffer 类,但不知道如何使用它来获得我想要的结果。请帮忙。

非常感谢

【问题讨论】:

  • Endian,不是印度。 en.wikipedia.org/wiki/Endianness
  • 打开鸡蛋可能会让人很困惑 :-)
  • 是的,你们是对的。匆忙你可以犯错误,但别担心,我会从我的错误中学到更多。感谢您指出我的错误。

标签: java


【解决方案1】:

打开 NIO FileChannel:

FileInputStream fs = new FileInputStream("myfile.bin");
FileChannel fc = fs.getChannel();

设置 ByteBuffer 字节序(用于 [get|put]Int()、[get|put]Long()、[get|put]Short()、[get|put]Double())

ByteBuffer buf = ByteBuffer.allocate(0x10000);
buf.order(ByteOrder.LITTLE_ENDIAN); // or ByteOrder.BIG_ENDIAN

从 FileChannel 读取到 ByteBuffer

fc.read(buf);
buf.flip();
// here you take data from the buffer by either of getShort(), getInt(), getLong(), getDouble(), or get(byte[], offset, len)
buf.compact();

要正确处理输入的字节顺序,您需要准确了解文件中存储的内容以及存储顺序(所谓的协议或格式)。

【讨论】:

    【解决方案2】:

    您可以从Apache Commons I/O 使用EndianUtils

    它有 static 方法,如 long readSwappedLong(InputStream input),可以为您完成所有交换。它还具有使用byte[] 作为输入的重载,以及write 对应项(OutputStreambyte[])。它还具有非 I/O 方法,例如 int swapInteger(int value) 方法,可以转换普通 Java 原语。

    该包还包含许多有用的实用程序类,例如FilenameUtilsIOUtils 等。

    另见

    【讨论】:

      【解决方案3】:

      下面的两个函数在 2 和 4 字节的字节顺序之间交换。

      static short Swap_16(short x) {
      
          return (short) ((((short) (x) & 0x00ff) << 8) | (((short) (x) & 0xff00) >> 8));
      }
      
      static int Swap_32(int x) {
          return ((((int) (x) & 0x000000ff) << 24)
                  | (((int) (x) & 0x0000ff00) << 8)
                  | (((int) (x) & 0x00ff0000) >> 8) | (((int) (x) & 0xff000000) >> 24));
      }
      

      【讨论】:

        【解决方案4】:

        我猜你应该每 4 个字节读取一次,然后简单地颠倒它们的顺序。

        【讨论】:

        【解决方案5】:

        在谷歌搜索这么多之后,我发现了一个具有 SwappedDataInputStream 类的 apache Jar 文件。 org.apache.commons.io.input.SwappedDataInputStream。 这门课使我的结果准确。有关该类的完整详细信息,请参阅。

        http://commons.apache.org/io/api-1.4/org/apache/commons/io/input/SwappedDataInputStream.html

        【讨论】:

          【解决方案6】:

          我最近写了一篇关于这样做的博客文章。关于如何在字节序之间转换二进制文件。将其添加到此处以供任何来这里的人将来参考。

          您可以通过以下简单代码完成此操作

          FileChannel fc = (FileChannel) Files.newByteChannel(Paths.get(filename), StandardOpenOption.READ);
          ByteBuffer byteBuffer = ByteBuffer.allocate((int)fc.size());
          byteBuffer.order(ByteOrder.BIG_ENDIAN);
          fc.read(byteBuffer);
          byteBuffer.flip();
          
          Buffer buffer = byteBuffer.asShortBuffer();
          short[] shortArray = new short[(int)fc.size()/2];
          ((ShortBuffer)buffer).get(shortArray);
          
          byteBuffer.clear();
          byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
          ShortBuffer shortOutputBuffer = byteBuffer.asShortBuffer();
          shortOutputBuffer.put(shortArray);
          
          FileChannel out = new FileOutputStream(outputfilename).getChannel();
          out.write(byteBuffer);
          out.close();
          

          有关其工作原理的详细信息,您可以参考原始博客文章 - http://pulasthisupun.blogspot.com/2016/06/reading-and-writing-binary-files-in.html

          或者代码可以在 - https://github.com/pulasthi/binary-format-converter

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-10-26
            • 2011-04-18
            • 2013-10-17
            • 1970-01-01
            • 2018-10-19
            • 2019-12-03
            相关资源
            最近更新 更多