gpt4 book ai didi

java - 带流的字节数组到 int 数组

转载 作者:行者123 更新时间:2023-12-01 19:50:02 24 4
gpt4 key购买 nike

我试图对带有流的字节数组进行按位操作,但它给出了错误“不可转换类型;无法将 '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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com