RandomAccessFile 字节顺序问题
RandomAccessFile Endianness issue
我必须从字节、整数和长数据大小的二进制文件中读取数据。我用RandomAccessFile
的方法readInt
、readLong
、readByte
读了它。问题是系统的字节顺序
(Windows 8.1) 和文件的字节顺序不同。例如文件中的这个数字实际上是数字2
,但从系统中读取为33554432
。
如何解决此问题并能够使用 RandomAccessFile
的方法读取数据块?
the problem is that the endianness of the system ... and the endianness of the file are different.
没有。他们是一样的。看数据。最低有效字节在开头。那就是little-endian,也就是Intel的endianness。 (不是 'Windows endianness',AFAIK 除了 w.r.t 之外不存在。一个特定的平台)。
问题是文件和系统的字节顺序是小端,但是RandomAccessFile
是大端。
在 Java 中解决此问题的方法是通过 NIO 和 ByteBuffer
,使用本机字节顺序而不是默认字节顺序。
我必须从字节、整数和长数据大小的二进制文件中读取数据。我用RandomAccessFile
的方法readInt
、readLong
、readByte
读了它。问题是系统的字节顺序
(Windows 8.1) 和文件的字节顺序不同。例如文件中的这个数字2
,但从系统中读取为33554432
。
如何解决此问题并能够使用 RandomAccessFile
的方法读取数据块?
the problem is that the endianness of the system ... and the endianness of the file are different.
没有。他们是一样的。看数据。最低有效字节在开头。那就是little-endian,也就是Intel的endianness。 (不是 'Windows endianness',AFAIK 除了 w.r.t 之外不存在。一个特定的平台)。
问题是文件和系统的字节顺序是小端,但是RandomAccessFile
是大端。
在 Java 中解决此问题的方法是通过 NIO 和 ByteBuffer
,使用本机字节顺序而不是默认字节顺序。