要列出的C#字节[]<;布尔>;
从布尔[]到字节[]: 但我需要将字节[]转换为列表,其中列表中的第一项是LSB 我尝试了下面的代码,但当再次转换为字节和布尔值时,我得到了两个完全不同的结果…:要列出的C#字节[]<;布尔>;,c#,byte,boolean,steganography,C#,Byte,Boolean,Steganography,从布尔[]到字节[]: 但我需要将字节[]转换为列表,其中列表中的第一项是LSB 我尝试了下面的代码,但当再次转换为字节和布尔值时,我得到了两个完全不同的结果…: public List<bool> Bits = new List<bool>(); public ToBools(byte[] values) { foreach (byte aByte in values) { for (int i
public List<bool> Bits = new List<bool>();
public ToBools(byte[] values)
{
foreach (byte aByte in values)
{
for (int i = 0; i < 7; i++)
{
Bits.Add(aByte.GetBit(i));
}
}
}
public static bool GetBit(this byte b, int index)
{
if (b == 0)
return false;
BitArray ba = b.Byte2BitArray();
return ba[index];
}
公共列表位=新列表();
公共ToBools(字节[]值)
{
foreach(值中的字节数)
{
对于(int i=0;i<7;i++)
{
bit.Add(aByte.GetBit(i));
}
}
}
公共静态bool GetBit(此字节b,int索引)
{
如果(b==0)
返回false;
位数组ba=b.Byte2BitArray();
返回ba[索引];
}
您只考虑7位,而不是8位。本说明:
for (int i = 0; i < 7; i++)
for(int i=0;i<7;i++)
应该是:
for (int i = 0; i < 8; i++)
for(int i=0;i<8;i++)
无论如何,我将如何实现它:
byte[] bytes = ...
List<bool> bools = bytes.SelectMany(GetBitsStartingFromLSB).ToList();
...
static IEnumerable<bool> GetBitsStartingFromLSB(byte b)
{
for(int i = 0; i < 8; i++)
{
yield return (b % 2 == 0) ? false : true;
b = (byte)(b >> 1);
}
}
byte[]bytes=。。。
List bools=bytes.SelectMany(GetBitsStartingFromLSB.ToList();
...
静态IEnumerable GetBitsStartingFromLSB(字节b)
{
对于(int i=0;i<8;i++)
{
收益率回报率(b%2==0)?假:真;
b=(字节)(b>>1);
}
}
您只考虑7位,而不是8位。本说明:
for (int i = 0; i < 7; i++)
for(int i=0;i<7;i++)
应该是:
for (int i = 0; i < 8; i++)
for(int i=0;i<8;i++)
无论如何,我将如何实现它:
byte[] bytes = ...
List<bool> bools = bytes.SelectMany(GetBitsStartingFromLSB).ToList();
...
static IEnumerable<bool> GetBitsStartingFromLSB(byte b)
{
for(int i = 0; i < 8; i++)
{
yield return (b % 2 == 0) ? false : true;
b = (byte)(b >> 1);
}
}
byte[]bytes=。。。
List bools=bytes.SelectMany(GetBitsStartingFromLSB.ToList();
...
静态IEnumerable GetBitsStartingFromLSB(字节b)
{
对于(int i=0;i<8;i++)
{
收益率回报率(b%2==0)?假:真;
b=(字节)(b>>1);
}
}
您为什么不将用于整个字节数组而不是每个单字节?您为什么不将用于整个字节数组而不是每个单字节?