0

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];
    }
0

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Just put .toArray() on the end and this will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.