Java byte array manipulation
-
01-03-2021 - |
Domanda
I am trying to select the lower 4 bits of the last byte in a byte array. This is how I have previously done it in PHP but I am new to Java.
$lower4bit = substr($bytes[19], -1);
//Convert the hex to decimal to get the offset value
$offset = hexdec($lower4bit);
//Select the value of the 4 bytes starting at the offset
$joinedArray = implode(array_slice($bytes, $offset, 4));
Can anyone point me in the correct direction with Java?
Soluzione
You access an array like so:
y = a[i];
You find the length of an array like so:
len = a.length;
You can isolate the last 4 bits of an integer like so:
y = x & 0xF;
These should be sufficient to construct the code that you need.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow