I'm currently working with some little-endian binary data, and I've reached an awkward point where I'm needing to convert odd numbers of bytes into integer values.
Now using the ByteBuffer class I'm able to read ints and longs perfectly fine using the getInt() getLong() functions, which read 4 and 8 bytes respectively.
However, in this example I need to read three bytes and make an int out of them. I've tried doing getShort + get() (2 bytes + 1 byte), but I don't think that's the correct way of doing it.
I'm guessing that I'll need to bit shift the bytes together in order to get the correct int value, but I always get a bit confused over bit shifting.
Also I would have thought that the bytebuffer class would have provided a function for reading odd numbers of bytes, but it seems not.
One way of doing it would be to create a byte[] of three bytes in length, put the three bytes into that, and then wrap a bytebuffer around it, and read an int from that. But that seems like a lot of extra code.
Any suggestions or advice would be appreciated.
Thanks