Reading little endian binary files

Hi,

Recently I tried to read from a little endian binary files. Here is the code:

FileInputStream file_input = new FileInputStream (file);

DataInputStream data_in= new DataInputStream (file_input );

while (true) {

try {

short i_data = data_in.readShort ();

}

catch (EOFException eof) {

System.out.println ("End of File");

break;

}

System.out.println ( i_data );

}

But I am getting wrong values (big-endian). I will be pleased if anyone can help me in this respect.

[559 byte] By [tushar_dea] at [2007-9-23]
# 1

Given your FileInputStream, get a FileChannel from it. Using the

FileChannel, map the entire file (or part of it) to a ByteBuffer. The

ByteBuffer can have its endianess set by the order method. Read your

shorts from this ByteBuffer. This is all nio stuff; check out the APIs.

kind regards,

Jos

JosAHa at 2007-7-11 > top of java,Core,Core APIs...
# 2

Hi,

Finally, I can do it easily with reading little endian binary files in java. Here is the code:

FileInputStream file_input = new FileInputStream (file);

DataInputStream data_in = new DataInputStream (file_input );

while (true) {

try {

int low = data_in.readByte() & 0xff;

int high = data_in.readByte() & 0xff;

short i_data = (short) (high << 8 | low) ;

}

catch (EOFException eof) {

System.out.println ("End of File");

break;

}

System.out.println ( i_data );

}

tushar_dea at 2007-7-11 > top of java,Core,Core APIs...