C#从字节数组中解析非均匀比特序列

时间:2014-11-19 15:49:06

标签: c# parsing bytearray

问题:是否有更有效的方法将字节数组中的位解析为整数值?如果是这样,会是什么?

数据当前是从流中读取的,其中包含一系列字节(以字节数组形式保存)。数据在这些字节中被压缩,使得值可以分布在多个字节上(按顺序)。构成值的位的大小取决于数据包的“类型”(保存在第一个字节的前5位(以MSB开头)。例如,

byte[] bytes = {0x73, 0xa4};
// yields: 0111001110100100
// vals:   [ 1 ][  2  ]3[4]

目前我使用的是扩展方法:

public static string ConvertToBinaryString(this byte[] bytes)
{
    return string.Join("", bytes.Select(x => Convert.ToString(x, 2).PadLeft(8, '0')));
}

将字节数组转换为二进制字符串。然后我使用这两种扩展方法之一将二进制字符串转换为int或int:

的数组
static readonly Regex IsBinary = new Regex("^[01]{1,32}$", RegexOptions.Compiled);
public static bool TryParseBits(this string toParse, int start, int length, out int intVal)
{
    intVal = -1;
    if (!IsBinary.IsMatch(toParse)) return false;
    if ((start + length + 1) > toParse.Length) return false;
    intVal = Convert.ToInt32(toParse.Substring(start, length), 2);
    return true;
}
public static bool TryParseBits(this string toParse, Queue<int> lengths, out List<int> vals)
{
    vals = new List<int>();
    if (!IsBinary.IsMatch(toParse)) return false;
    var idx = 0;
    while (lengths.Count > 0)
    {
        var l = lengths.Dequeue();
        if ((idx + l) > toParse.Length) return false;
        vals.Add(Convert.ToInt32(toParse.Substring(idx, l), 2));
        idx += l;
    }
    return true;
}

使用示例:

int type;
var success = "0111001110100100".TryParseBits(0, 5, out type);

结果为type = 14

背景:正在读取的流最多可以提供12个数据包/秒。在必须解析字节之前发生了一些预处理,并且在值上发生了重要的后处理。使用Parallel.ForEach将数据包分成四个线程。这些值永远不会超过28位,因此转换为int时我不担心符号位。

3 个答案:

答案 0 :(得分:1)

你试过像bitmask这样的东西吗? 知道例如第一个byteArray的最后4位是我们的第一个val,我们可以这样做:

byte[] bytes = { 0x73, 0xa4 };
int v1 = bytes[0] & 0x0F;
//second val:
int v2 = bytes[2] & 0xF0;

OR
在应用面具之前,只需将所有内容存储在较大的nr。

 int total = 0;
 total = total | bytes[0];
 total = total << 8;
 total = total | bytes[1];
 //now the 2 bytes array is stored in a number in our case total will be: 111001110100100
 //after storing apply bit masks as described above:
 // to get the LAST 3 bytes
 int var1 = total & 0x7 //mask with last 3 bytes
 total = total >> 3; //take out last 3 bytes
 int var2 = total & 0x1 //last byte.
 total = total >>1;
 //and so on

答案 1 :(得分:1)

为了完整性,这是我根据明显删除的答案提出的。

public static int ParseBits(this byte[] bytes, int start, int length)
{
    // Need to reverse the array to make it usable with BitArray
    Array.Reverse(bytes);
    var ba = new BitArray(bytes);
    var idx = 0;
    var shft = length - 1;
    // Iterate backwards through the bits and perform bitwise operations
    for (var i = start + length - 1; i >= 0; i--)
    {
        idx |= (Convert.ToInt32(ba.Get(i)) << shft);
        shft--;
    }
    return idx;
}

答案 2 :(得分:0)

你只需要了解按位操作。

byte[] bytes = {0x73, 0xa4};
// yields: 0111001110100100
// vals: [ 1 ][ 2 ]3[4]

int bits = bytes[1] | bytes [0] << 8;
int v1 = (bits & 0xf800) >> 11;
int v2 = (bits & 0x07f0) >> 5;
int v3 = (bits & 0x0008) >> 3;
int v4 = bits & 0x0007;