C# 如何将 short[] 转换为 bool[]?
C# How convert short[] to bool[]?
short[] sBuf = new short[2];
sBuf[0] = 1;
sBuf[1] = 2;
bool[] bBuf = new bool[sBuf.Length * 16];
Buffer.BlockCopy(sBuf, 0, bBuf, 0, sBuf.Length * 2);
Desired result value
sBuf[0] = 1
bBuf[0] = true, bBuf[1] = false, bBuf[2] = false, bBuf[3] = false...
sBuf[0] = 2
bBuf[16] = false, bBuf[17] = true, bBuf[18] = false, bBuf[19] = false...
但无法正确转换
我想从 short [] 转换为 bool [],但我不知道如何。
从 Convert.ToBoolean
的 msdn 页面上说,每个 0
值都将转换为 false
,每个非 0
值都将转换为 true
:
bool[] bBuf = new bool[sBuf.Length];
for(int i = 0; i < sBuf.Length; ++i )
{
bBuf[i] = Convert.ToBoolean(sBuf[i]);
}
编辑:
根据 short
值中设置的位设置你的 bool[]
你可以使用这个:
const int BITS_PER_BYTE = 8; // amount of bits per one byte
int elementBitsSize = sizeof(short) * BITS_PER_BYTE; // size in bits of one element in array
bool[] bBuf = new bool[sBuf.Length * elementBitsSize]; // new array size
// iterate through each short value
for ( int i = 0; i < sBuf.Length; ++i )
{
// iterate through bits in that value
for( int j = 0; j < elementBitsSize; ++j )
{
// assign new value
bBuf[j + i * elementBitsSize] = Convert.ToBoolean(sBuf[i] & (1 << j));
}
}
假设每个 bool
代表其对应的 short
中的一个位(这可能是您将大小乘以 16 的原因),您可以按如下方式进行转换:
bBuf = sBuf
.SelectMany(s => Enumerable.Range(0, 16).Select(i => (s & (1<<i)) != 0))
.ToArray();
想法是通过调用 Enumerable.Range
、用 (1 << i)
屏蔽数字并将结果与零进行比较,为每个 short
构造 16 个布尔值。
// Convert short to bool
bool[] bBuf = Array.ConvertAll(sBuf, n => n != 0);
// Convert short to bit representation bool array
bool[][] bBuf= Array.ConvertAll(arr, n =>
{
bool[] bits = new bool[16];
for (int index = 0; index < 16; index++)
{
bits[index] = (n & (1 << index)) > 0;
}
return bits;
});
short[] sBuf = new short[2];
sBuf[0] = 1;
sBuf[1] = 2;
bool[] bBuf = new bool[sBuf.Length * 16];
Buffer.BlockCopy(sBuf, 0, bBuf, 0, sBuf.Length * 2);
Desired result value
sBuf[0] = 1
bBuf[0] = true, bBuf[1] = false, bBuf[2] = false, bBuf[3] = false...
sBuf[0] = 2
bBuf[16] = false, bBuf[17] = true, bBuf[18] = false, bBuf[19] = false...
但无法正确转换
我想从 short [] 转换为 bool [],但我不知道如何。
从 Convert.ToBoolean
的 msdn 页面上说,每个 0
值都将转换为 false
,每个非 0
值都将转换为 true
:
bool[] bBuf = new bool[sBuf.Length];
for(int i = 0; i < sBuf.Length; ++i )
{
bBuf[i] = Convert.ToBoolean(sBuf[i]);
}
编辑:
根据 short
值中设置的位设置你的 bool[]
你可以使用这个:
const int BITS_PER_BYTE = 8; // amount of bits per one byte
int elementBitsSize = sizeof(short) * BITS_PER_BYTE; // size in bits of one element in array
bool[] bBuf = new bool[sBuf.Length * elementBitsSize]; // new array size
// iterate through each short value
for ( int i = 0; i < sBuf.Length; ++i )
{
// iterate through bits in that value
for( int j = 0; j < elementBitsSize; ++j )
{
// assign new value
bBuf[j + i * elementBitsSize] = Convert.ToBoolean(sBuf[i] & (1 << j));
}
}
假设每个 bool
代表其对应的 short
中的一个位(这可能是您将大小乘以 16 的原因),您可以按如下方式进行转换:
bBuf = sBuf
.SelectMany(s => Enumerable.Range(0, 16).Select(i => (s & (1<<i)) != 0))
.ToArray();
想法是通过调用 Enumerable.Range
、用 (1 << i)
屏蔽数字并将结果与零进行比较,为每个 short
构造 16 个布尔值。
// Convert short to bool
bool[] bBuf = Array.ConvertAll(sBuf, n => n != 0);
// Convert short to bit representation bool array
bool[][] bBuf= Array.ConvertAll(arr, n =>
{
bool[] bits = new bool[16];
for (int index = 0; index < 16; index++)
{
bits[index] = (n & (1 << index)) > 0;
}
return bits;
});