将ushort数组转换为位数组



我有一个吸管数组:

ushort[] testArray = new ushort[]{1, 2, 3, 4, 5};

如何将其转换为一些数组。在上面的示例中,5 * 16位数组的长度?预先感谢!

非常简单的Google搜索提供此BitArray类

示例

using System.Collections;
int[]  myInts  = new int[5] { 6, 7, 8, 9, 10 };
BitArray myBA5 = new BitArray( myInts );

更新:

所以我对这个问题感兴趣,并决定完成一个工作解决方案。这就是我要处理短裤的方法。一件事是,我假设钻头仍然会翻译,因为您没有尝试重新拦截(??),而只是想循环穿过原始位?

var shorts = new ushort[] { 5, 1 };
var numberOfInts = (int)Math.Ceiling((decimal)shorts.Length / 2);
var theInts = new int[numberOfInts];
var shortsPos = 0;
for (int i = 0; i < numberOfInts; i++)
{   
    theInts[i] = shorts[shortsPos + 1];
    theInts[i] = theInts[i] | ((int)shorts[shortsPos] << 16);
    shortsPos =+ 2;
    //theInts[i].Dump();
}
var bitArr = new BitArray(theInts);
foreach (var bit in bitArr)
{
    //bit.Dump();
}

我在linqpad中对此进行了嘲笑,因此.dump()语句,所以您可以自己的事物进行测试。您可能必须在订单上切换,也许在每对中转移第二个...我将离开该部分供您锻炼。另外,如果您在源ushort中只有一个值[],您会遇到错误

上述结果是:

True
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
True
False
True
False
False
False
False
False
False
False
False
False
False
False
False
False
            int[] testArray = new int[] { 1, 2, 3, 4, 5 };
            Dictionary<int, int[]> convertData = new Dictionary<int, int[]>();
            foreach (int x in testArray)
            {
                System.Collections.BitArray b = new System.Collections.BitArray(Convert.ToByte(x));
                convertData[x] = b.Cast<bool>().Select(bit => bit ? 1 : 0).ToArray();
            }

最新更新