Extending from converting an array of signed bytes to unsigned bytes, can this be performed more elegantly without the clunky for loop using Lambdas? So,
signed byte[]
-> unsigned int[]
I note that there is no Arrays.stream(byte[])
.
Extending from converting an array of signed bytes to unsigned bytes, can this be performed more elegantly without the clunky for loop using Lambdas? So,
signed byte[]
-> unsigned int[]
I note that there is no Arrays.stream(byte[])
.
Sure, you can:
int[] unsigned = IntStream.range(0, signed.length)
.map(i -> signed[i] & 0xFF)
.toArray();
where signed represents the array of bytes.
Yes, you could achieve it using IntStream
:
public static int[] toUnsignedIntArray(byte[] array) {
return IntStream.range(0, array.length)
.map(idx -> array[idx] & & 0xFF)
.toArray();
}
For more information, see this answer.
You could do it like this.
int[] unsigned = IntStream.range(0, bytes.length)
.map(i -> 0x7F & bytes[i])
.toArray();