简体   繁体   中英

Convert BitArray to a small byte array

I've read the other posts on BitArray conversions and tried several myself but none seem to deliver the results I want.

My situation is as such, I have some c# code that controls an LED strip. To issue a single command to the strip I need at most 28 bits

1 bit for selecting between 2 led strips

6 for position (Max 48 addressable leds)

7 for color x3 (0-127 value for color)

Suppose I create a BitArray for that structure and as an example we populate it semi-randomly.

        BitArray ba = new BitArray(28);

        for(int i = 0 ;i < 28; i++)
        {
            if (i % 3 == 0)
                ba.Set(i, true);
            else
                ba.Set(i, false);
        }

Now I want to stick those 28 bits in 4 bytes (The last 4 bits can be a stop signal), and finally turn it into a String so I can send the string via USB to the LED strip.

All the methods I've tried convert each 1 and 0 as a literal char which is not the goal.

Is there a straightforward way to do this bit compacting in C#?

Well you could use BitArray.CopyTo :

byte[] bytes = new byte[4];
ba.CopyTo(bytes, 0);

Or:

int[] ints = new int[1];
ba.CopyTo(ints, 0);

It's not clear what you'd want the string representation to be though - you're dealing with naturally binary data rather than text data...

I wouldn't use a BitArray for this. Instead, I'd use a struct, and then pack that into an int when I need to:

struct Led
{
    public readonly bool Strip;
    public readonly byte Position;
    public readonly byte Red;
    public readonly byte Green;
    public readonly byte Blue;

    public Led(bool strip, byte pos, byte r, byte g, byte b)
    {
        // set private fields
    }

    public int ToInt()
    {
        const int StripBit = 0x01000000;
        const int PositionMask = 0x3F; // 6 bits
        // bits 21 through 26
        const int PositionShift = 20;
        const int ColorMask = 0x7F;
        const int RedShift = 14;
        const int GreenShift = 7;

        int val = Strip ? 0 : StripBit;
        val = val | ((Position & PositionMask) << PositionShift);
        val = val | ((Red & ColorMask) << RedShift);
        val = val | (Blue & ColorMask);
        return val;
    }
}

That way you can create your structures easily without having to fiddle with bit arrays:

var blue17 = new Led(true, 17, 0, 0, 127);
var blah22 = new Led(false, 22, 15, 97, 42);

and to get the values:

int blue17_value = blue17.ToInt();

You can turn the int into a byte array easily enough with BitConverter :

var blue17_bytes = BitConverter.GetBytes(blue17_value);

It's unclear to me why you want to send that as a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM