Javascript byte array to Int32Array equivalent in Java

Viewed 16

I have a javascript snippet which will take 4 byte and convert that in to int value.

var bufView = new Uint8Array([0xe4, 0x0e, 0x00, 0x00]);
var a = new Int32Array(bufView.buffer.slice(0,4))[0]
console.log(a);

Output of the above code is 3812

I want to do the same operation in Java as well where I have

byte[] bytes = {(byte) 0xe4, 0x0e, 0x00, 0x00};

I have searched whole internet but I could not see any results related to this. How can we achieve this?

1 Answers

As Ilya Bursov commented, I could able to achieve the same using the following Java code snippet.

byte[] bytes = {(byte) 0xe4, 0x0e, 0x00, 0x00};
int anInt = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getInt();
System.out.println(anInt);
Related