Following the similar question, I am trying to implement the unpack method in Java for Integer:
Sample byte array input: chunkByte[1219:1240] = [64, 30, 31, 0, 64, 118, 31, 0, 64, -22, 29, 0, 64, 4, 30, 0, 64, 26, 36, 0, 64] where
Code in Python:
fid = open(raw_file, 'rb')
fid.seek(0, os.SEEK_END)
nFileLen = fid.tell()
fid.seek(0, 0)
chunk_size = 2 ** 19
n = int(nFileLen / chunk_size)
for i in range(0, n):
nLenVals = round(chunk_size / 4)
vals = struct.unpack('I' * nLenVals, fid.read(chunk_size))
In Java:
vals = (int[]) unpack('I', chunk_size / 4, chunkByte);
where
private Object unpack(char type, int dim, byte[] chunkByte) {
if (type == 'f') {
var floats = ByteBuffer.wrap(chunkByte).order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer();
var floatArray = new float[dim];
floats.get(floatArray);
return floatArray;
} else if (type == 'I') {
var ints = ByteBuffer.wrap(chunkByte).order(ByteOrder.nativeOrder()).asIntBuffer();
var intArray = new int[dim];
ints.get(intArray);
return intArray;
}
return null;
}
I get negative results for the same byte arrays (byte[] chunkByte).
For example, in Python, I get this value: vals[1220]=3221234265. But I get the wrong value for the similar index: vals[1220]=-1073733031.
I tried changing nativeOrder() to LITTLE_ENDIAN but it didn't help.