简体   繁体   中英

C and Python code to Java help

I'm trying to read in some bytes from a file in Java and then create bitmasks from some of the data and lengths and offsets from others.

I'm so close to getting my program working but I keep getting garbled data coming out the other end.

I'm 90% sure my problem is something to do with the way that Java is reading the bytes in.

There is some Python and C code that I'm basing my design on but I don't know how to convert this into Java. I've tried wrapping a byte[] in a byteBuffer but I'm still getting confusing results.

This is what I'm trying to get my head around:

bitmask:= copy inputBuffer[inputIndex] as 32-bit integer in little-endian format - needs to be four bytes

In C this is done as:

bitmask= (inputBuffer[inputIndex + 3] << 24) | (inputBuffer[inputIndex + 2] << 16) |
                        (inputBuffer[inputIndex + 1] <<  8) | inputBuffer[inputIndex];

In Python this is: bitmask= unpack("<L", inputBuffer[inputIndex:inputIndex + 4])[0]

Until I can get this working correctly my program is falling over.

Can anyone offer any information on the best way of implementing this?

Many thanks

Tony

您可以将ByteBuffer的byteOrder设置为littleEndian,而只需使用getInt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM