Java – convert small endian files to large endian files
•
Java
How to convert a liitle endian binary file into a large endian binary file I have a binary file written in C. I read this file with datainputstream in Java and read it into greater India format I've also seen the ByteBuffer class, but I don't know how to use it to get the results I want Please help.
Thank you.
Solution
Open NiO filechannel:
FileInputStream fs = new FileInputStream("myfile.bin"); FileChannel fc = fs.getChannel();
Set ByteBuffer endianness (used by [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
Read ByteBuffer from filechannel
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();
To correctly handle the byte order of input, you need to know exactly what is stored in the file and in what order (so-called protocol or format)
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码