【发布时间】:2011-06-12 16:13:27
【问题描述】:
private int bitToIntParser
(byte[] recordData, int byteOffset, int byteLength, int bitOffset, int bitLength)
{
//step1:Byte[] selectedBytes = recordData[byteOffset to byteOffset + Length]
//step2:BitArray selectedBits=selectdBytes.bits[bitOffset to bitOffset+bitLength]
//step3:convert selectedBit to Int
}
上面的函数应该可以从recordData中提取bytes[byteOffset]到bytes[byteOffset+length],然后再从之前的结果中提取bit[bitOffset]到bit[bitOffset+BitLength],再转换成int。
谁能帮我解决这个问题?
【问题讨论】:
-
对于第 1 步,我使用了 byte[] workingSet = new byte[4]; Buffer.BlockCopy(recordData, byteOffset, workingSet, 0, length);但后来我不确定如何从中提取请求的位,我尝试移位但无法获得结果
-
对 bitOffset 和 bitLength 参数有什么限制吗?
-
@Henk : 它不应该超过 32 位,所以我们可以使用 BitVector32
-
这样输入的正确结果是什么?记录数据:{00001111b 11110000b} byteOffset:0 byteLength:2 bitOffset:4 bitLength:8
-
标签: c# .net binary bit-manipulation math