【问题标题】:get every nth byte/value in array获取数组中的每个第 n 个字节/值
【发布时间】:2023-03-09 01:32:01
【问题描述】:

我正在寻找一种平滑/快速的方法来检索字节数组中的每第 n 个短并将其复制到一个新数组中。

l = 低字节 u = 大字节

我的数据格式如下: byte[] bytes= {a0l, a0u, b0l, b0u, c0l, ..., n0l, n0l, a1l, a1u, b1l, ..., nXl, nXu}

我需要的是获得长度为 X 的 n 字节数组(例如,a[0..X]、b[0..X]、... 或 M[a..n][0.. X])

我在考虑以下两个步骤:

  1. 将值转换为short (=> short[] shorts= { a0, b0, c0, ... n0, a1, .. nX}) 通过使用类似的东西

        short[] shorts= new short[(int)(Math.Ceiling(bytes.Length / 2.0)];
        Buffer.BlockCopy(bytes, 0, shorts, 0, bytes.Length);
    
  2. 从短裤中检索每一秒的值

       I'm looking for some fast code here... something like blockcopy with skip
       I am completely aware that I could use a loop - but maybe there's a better
       solution to it as I need to do this task for 80MB/s...
    
  3. 将其转换回字节数组(相同 - 使用块复制)

        byte[] arrayX = new byte[shorts.Length * 2];
        Buffer.BlockCopy(shorts, 0, arrayX , 0, arrayX .Length);
    

非常感谢!

【问题讨论】:

    标签: c# bytearray


    【解决方案1】:

    我认为您最好将字节直接复制到新的字节数组,计算出每个短的正确偏移量。

    将其全部转换为单独的短数组并返回字节数组是浪费时间 IMO。

    【讨论】:

    • 你是完全正确的——这实际上是我所希望的——但是我需要一种快速的方法来每 n 次复制 2 个数据包。通过转换为 short 我不需要它,而是希望有一种快速的方法来查看/获取每个 nth 值
    猜你喜欢
    • 2018-11-15
    • 2013-11-28
    • 2021-10-05
    • 1970-01-01
    • 2011-12-08
    • 2020-07-21
    • 2022-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多