我试图对带有流的字节数组进行按位操作,但它给出了错误“不可转换类型;无法将 '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];
}
最佳答案
Java 没有字节流。但您可以改为迭代索引:
IntStream.range(0, packet.length)
.map(i -> packet[i] & 0xff)
除非你正在做一些繁重的处理,否则我不会费心使用parallel()
。
关于java - 带流的字节数组到 int 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51681189/