質問

私はList<bool>に変換するbyte[]を持っています。私はこれをどのように行うのですか? list.toArray()bool[]を作成します。

役に立ちましたか?

解決

ここでは、バイトにビットをパック、または元のビットのように多くのバイトをしたいかに応じて、二つのアプローチです:

    bool[] bools = { true, false, true, false, false, true, false, true,
                     true };

    // basic - same count
    byte[] arr1 = Array.ConvertAll(bools, b => b ? (byte)1 : (byte)0);

    // pack (in this case, using the first bool as the lsb - if you want
    // the first bool as the msb, reverse things ;-p)
    int bytes = bools.Length / 8;
    if ((bools.Length % 8) != 0) bytes++;
    byte[] arr2 = new byte[bytes];
    int bitIndex = 0, byteIndex = 0;
    for (int i = 0; i < bools.Length; i++)
    {
        if (bools[i])
        {
            arr2[byteIndex] |= (byte)(((byte)1) << bitIndex);
        }
        bitIndex++;
        if (bitIndex == 8)
        {
            bitIndex = 0;
            byteIndex++;
        }
    }

他のヒント

あなたは、LINQを使用することができます。これは効率的ではありませんが、シンプルになります。私はあなたがBOOLにつき1つのバイトをしたいと仮定しています。

bool[] a = new bool[] { true, false, true, true, false, true };
byte[] b = (from x in a select x ? (byte)0x1 : (byte)0x0).ToArray();

マルクの答えはすでに良いですが、...

のあなたは、ビットいじるをやって快適である人物の一種である、または単に少ないコードを書きたいと仮定すると、のいくつかのより多くのパフォーマンス、そしてこのここにコードを絞り出しますあなたのために良いサー/マダムです。

byte[] PackBoolsInByteArray(bool[] bools)
{
    int len = bools.Length;
    int bytes = len >> 3;
    if ((len & 0x07) != 0) ++bytes;
    byte[] arr2 = new byte[bytes];
    for (int i = 0; i < bools.Length; i++)
    {
        if (bools[i])
            arr2[i >> 3] |= (byte)(1 << (i & 0x07));
    }
}

それはちょうどより簡潔だ、マルクのコードとまったく同じことを行います。

もちろん、我々はを場合は、実際には、我々はあまりにも、それをアンロールことができ、すべての外出したい...
...と戻り値の型にカーブボールを投げることができます、我々はそれでありながら、!

IEnumerable<byte> PackBoolsInByteEnumerable(bool[] bools)
{
    int len = bools.Length;
    int rem = len & 0x07; // hint: rem = len % 8.

    /*
    byte[] byteArr = rem == 0 // length is a multiple of 8? (no remainder?)
        ? new byte[len >> 3] // -yes-
        : new byte[(len >> 3)+ 1]; // -no-
     */

    const byte BZ = 0,
        B0 = 1 << 0, B1 = 1 << 1, B2 = 1 << 2, B3 = 1 << 3,
        B4 = 1 << 4, B5 = 1 << 5, B6 = 1 << 6, B7 = 1 << 7;

    byte b;
    int i = 0;
    for (int mul = len & ~0x07; i < mul; i += 8) // hint: len = mul + rem.
    {
        b = bools[i] ? B0 : BZ;
        if (bools[i + 1]) b |= B1;
        if (bools[i + 2]) b |= B2;
        if (bools[i + 3]) b |= B3;
        if (bools[i + 4]) b |= B4;
        if (bools[i + 5]) b |= B5;
        if (bools[i + 6]) b |= B6;
        if (bools[i + 7]) b |= B7;

        //byteArr[i >> 3] = b;
        yield return b;
    }

    if (rem != 0) // take care of the remainder...
    {
        b = bools[i] ? B0 : BZ; // (there is at least one more bool.)

        switch (rem) // rem is [1:7] (fall-through switch!)
        {
            case 7:
                if (bools[i + 6]) b |= B6;
                goto case 6;
            case 6:
                if (bools[i + 5]) b |= B5;
                goto case 5;
            case 5:
                if (bools[i + 4]) b |= B4;
                goto case 4;
            case 4:
                if (bools[i + 3]) b |= B3;
                goto case 3;
            case 3:
                if (bools[i + 2]) b |= B2;
                goto case 2;
            case 2:
                if (bools[i + 1]) b |= B1;
                break;
            // case 1 is the statement above the switch!
        }

        //byteArr[i >> 3] = b; // write the last byte to the array.
        yield return b; // yield the last byte.
    }

    //return byteArr;
}

ヒント:あなたは、私がコメントとしてbyte[]を返すためのコードが含まれて見ることができるように。それはあなたが何をしたい/必要であれば、単純に代わり2つの歩留まり文をコメントアウトします。

<時間>

をいじるヒント:x >> 3をシフト
安いx / 8です。 x & 0x07をマスキング
安いx % 8です。 x & ~0x07をマスキング
安いx - x % 8です。

<時間>

編集を ここではいくつかの例のドキュメントがあります:

    /// <summary>
    /// Bit-packs an array of booleans into bytes, one bit per boolean.
    /// </summary><remarks>
    /// Booleans are bit-packed into bytes, in order, from least significant
    /// bit to most significant bit of each byte.<br/>
    /// If the length of the input array isn't a multiple of eight, then one
    /// or more of the most significant bits in the last byte returned will
    /// be unused. Unused bits are zero / unset.
    /// </remarks>
    /// <param name="bools">An array of booleans to pack into bytes.</param>
    /// <returns>
    /// An IEnumerable&lt;byte&gt; of bytes each containing (up to) eight
    /// bit-packed booleans.
    /// </returns>

あなたがリストの種類以上の任意のコントロールを持っている場合は、それに続いてToArrayのバイト[]が生成されます一覧を作成してみてください()。あなたはArrayListのを持っている場合は、使用することができます:

(byte[])list.ToArray(typeof(byte));
リストを取得するには、

、あなたは)コンストラクタへの入力としてご指定されていないリスト反復子を持つものを作成し、次にてToArrayを(作り出すことができますか?またはブール値から新しいバイトにキャスト、各項目をコピーする?

それが役立つかもしれないですリストのどのような種類のいくつかの情報。

BitConverter のクラスを見てください。あなたの条件の正確な性質に応じて、それはかなりきちんとあなたの問題を解決することがあります。

別のLINQのアプローチ、@ hfcs101さんよりも効果がなく、簡単にだけでなく、他の値型のために働くだろう。

var a = new [] { true, false, true, true, false, true };
byte[] b = a.Select(BitConverter.GetBytes).SelectMany(x => x).ToArray();

またはAnorZakenの答えをhref="https://stackoverflow.com/a/36572780/5185376"> IEnumerableアプローチます:

static IEnumerable<byte> PackBools(IEnumerable<bool> bools)
{
    int bitIndex = 0;
    byte currentByte = 0;
    foreach (bool val in bools) {
        if (val)
            currentByte |= (byte)(1 << bitIndex);
        if (++bitIndex == 8) {
            yield return currentByte;
            bitIndex = 0;
            currentByte = 0;
        }
    }
    if (bitIndex != 8) {
        yield return currentByte;
    }
}

そしてpaddingEndをアンパックする最後のバイトから廃棄するビットの量を意味するに従って開梱

static IEnumerable<bool> UnpackBools(IEnumerable<byte> bytes, int paddingEnd = 0)
{
    using (var enumerator = bytes.GetEnumerator()) {
        bool last = !enumerator.MoveNext();
        while (!last) {
            byte current = enumerator.Current;
            last = !enumerator.MoveNext();
            for (int i = 0; i < 8 - (last ? paddingEnd : 0); i++) {
                yield return (current & (1 << i)) != 0;
            }
        }
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top