Matahari
Matahari

Reputation: 109

Byte array to int array with streams

I am trying to do a bitwise operation on a byte array with streams but it gives me the error that says "Inconvertible types; cannot cast 'byte[]' to 'int'". I want to bitwise every item in the byte array with 0xff via streams.

byte[] packet;
//this does not work
Stream.of(packet).parallel().map(e->(int)e&0xff)//then put int into an  array or list

//basically this is what I am trying to do with streams
    int[] castedValues = new int[packet.length];
    for (int i = 0; i < packet.length; i++) {
        castedValues[i] = packet[i];
    }

Upvotes: 0

Views: 168

Answers (1)

shmosel
shmosel

Reputation: 50716

Java doesn't have a byte stream. But you can iterate over the indexes instead:

IntStream.range(0, packet.length)
        .map(i -> packet[i] & 0xff)

I wouldn't bother with parallel() unless you're doing some heavy processing.

Upvotes: 3

Related Questions