【问题标题】:Byte array to int array with streams字节数组到带流的 int 数组
【发布时间】:2018-08-03 23:32:15
【问题描述】:

我正在尝试对带有流的字节数组进行按位运算,但它给我的错误是“不可转换的类型;不能将 '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 arrays java-stream bitwise-and


    【解决方案1】:

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

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

    除非您进行繁重的处理,否则我不会打扰 parallel()

    【讨论】:

    • 只需将.toArray() 放在末尾即可。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 2011-07-31
    • 2016-04-05
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多