簡単な質問: bool (true, false) が bit (1, 0) と同じであることを考えると、C# で 8 つの bool を 1 バイトに変換する正しい方法は何ですか?
例:
真、真、真、真、真、真、真、真 == 11111111 == 255
真、偽、偽、偽、偽、偽、偽、偽 == 10000000 == 128
偽、偽、偽、偽、偽、偽、偽、偽 == 00000000 == 0
以上が第一部です。そのような拡張メソッドを作成したい:
public static byte[] ToByteArray(this bool[] bitArray)
{
// stuff here
return byteArray;
}
結果は、bool 配列の 8 分の 1 の要素を含む byteArray でなければなりません。