字节数组到带流的int数组

时间:2018-08-03 23:32:15

标签: java arrays java-stream bitwise-and

我正在尝试对具有流的字节数组进行按位运算,但它给我的错误是“不可转换类型;无法将'byte []'强制转换为'int'”。我想通过流按位与0xff字节数组中的每个项目。

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

1 个答案:

答案 0 :(得分:3)

Java没有字节流。但是您可以改为遍历索引:

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

除非您要进行大量处理,否则我不会为parallel()所困扰。