Is there an existing Java class to convert data stored little-endian to integers by offset and length?

0

I have a data file that is made up of various fields in little-endian order. I know what the offset and length are for each of the various fields. Is there an existing Java class that will convert by offset and length to an integer? For example: 17 00 00 00 53 43 43 41 11 00 00 00 AA 35 00 00. Bytes 1 thru 4 (17 00 00 00) is 0x00000017.

java
converter
endianness
asked on Stack Overflow Jan 30, 2015 by J Wright

4 Answers

0

Guava provides the LittleEndianDataInputStream class which can be used to retrieve values in little endian order.

answered on Stack Overflow Jan 30, 2015 by Andy Turner
0

You can do that with a java.nio.IntBuffer or java.nio.ByteBuffer, in association with a java.nio.channels.FileChannel to read the data.

answered on Stack Overflow Jan 30, 2015 by user207421
-1

You should try to read it as it is. Get block of 4 bytes and treat them as integers. I'm sure JVM already treats numbers little endian (as far as I know, all Intel/amd are so).

answered on Stack Overflow Jan 30, 2015 by tuttoweb
-1

If your file is a file of raw bytes you can use ByteBuffer to read the file in little endian mode, and then use asIntBuffer() to read out the ints through an IntBuffer. If you need to navigate the file, you can use srcChan.position(targetPosition); to skip to your next "field".

try (RandomAccessFile srcFile = new RandomAccessFile("data/data.bin", "r");
     FileChannel srcChan = srcFile.getChannel();) 
{
    //    Careful with these casts if you have large files - channel size is a long
    ByteBuffer ib = ByteBuffer.allocate((int)srcChan.size());
    ib.order(ByteOrder.LITTLE_ENDIAN);
    srcChan.read(ib);
    IntBuffer fb = ((ByteBuffer)ib.rewind()).asIntBuffer();
    while (fb.hasRemaining())
        System.out.println(fb.get());
}

Output:

23
1094927187
17
13738

If however you have a text file with a series of space delimited hex strings (the file format is unclear from your question) you'll have to add a bit of trickery to read that in, but once it is in you can use ByteBuffer in little-endian mode to read out the integers again.

The general process would be:

  1. inputString.split(" ")
  2. convert each element of that String array into a byte[]
  3. use ByteBuffer.wrap to wrap the resulting byte[] and walk through the ints

It probably (not compile time tested) looks something like the below (which doesn't use an IntBuffer, just to show an alternative method):

String input = "17 00 00 00 53 43 43 41 11 00 00 00 AA 35 00 00";
String[] source = input.split(" ");
byte[] sourceBytes = new byte[source.length];
for (int i = 0; i < source.length; i++) {
    sourceBytes[i] = (byte)Integer.parseInt(source[i], 16);
}
ByteBuffer bb = ByteBuffer.wrap(sourceBytes);
bb.order(ByteOrder.LITTLE_ENDIAN);
while (bb.hasRemaining())
    System.out.println(bb.getInt());

The output is the same as for the first approach.

answered on Stack Overflow Jan 30, 2015 by Andy Brown • edited Jan 31, 2015 by Andy Brown

User contributions licensed under CC BY-SA 3.0