Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 位数组中的拆分字节数组_C#_Bytearray_Bit Manipulation - Fatal编程技术网

C# 位数组中的拆分字节数组

C# 位数组中的拆分字节数组,c#,bytearray,bit-manipulation,C#,Bytearray,Bit Manipulation,我有一个长度为64的字节数组。我有这个字节数组中的数据对应的顺序列表。这个列表告诉我每个值的大小(以位为单位)。大多数值是8或16位,很容易解析出来。然而,大约在列表的中间,我开始得到12位或5位的长度。通过这些循环并提取所需位的最佳方法是什么。以下代码应从存储为uint[]的数据缓冲区中提取n位。。我还没有测试过,所以请注意莱克托 uint GetBits(uint[]数据,参考uint pos,uint n){ uint a=位置/32; uint off=位置%32; uint掩码=(1(

我有一个长度为64的字节数组。我有这个字节数组中的数据对应的顺序列表。这个列表告诉我每个值的大小(以位为单位)。大多数值是8或16位,很容易解析出来。然而,大约在列表的中间,我开始得到12位或5位的长度。通过这些循环并提取所需位的最佳方法是什么。

以下代码应从存储为
uint[]的数据缓冲区中提取n位。
。我还没有测试过,所以请注意莱克托

uint GetBits(uint[]数据,参考uint pos,uint n){
uint a=位置/32;
uint off=位置%32;
uint掩码=(1(32-关))&掩码;
}

请注意,它假定很少的尾端存储,因此未对齐的值从一个字的高位流入下一个字的低位。

是否有一个包含数据的字节数组,并希望根据另一个数组将其拆分为5/8/12/16位块?拆分为一个ushort/uint数组?还是什么?@erikH-我正在查看的两个文件中的位数大小不同,但它们是在头文件中定义的。然后,此标题后面跟着数据。要读取数据,我可以通过参考听者信息查看头部,如果有意义的话。无论如何,我只是想找出解决这个问题的最佳方法。
uint GetBits(uint[] data, ref uint pos, uint n) {
    uint a = pos/32;
    uint off = pos%32;
    uint mask = (1 << n) - 1;
    pos += n;
    return (data[a] << off)&mask | (data[a + 1] >> (32 - off))&mask;
}