• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2811
  • Last Modified:

C# - Convert byte[] to bit mask


I'm writing an web app which contains a large series of Checkboxes on a page.  Once the user selects which options he/she wants I am creating a byte array like this:

//Just using 4 for simplicity:
byte[] options = new byte[4]
options[0] = Convert.ToByte(cbOption1.Checked);
options[1] = Convert.ToByte(cbOption2.Checked);
options[2] = Convert.ToByte(cbOption3.Checked);
options[3] = Convert.ToByte(cbOption4.Checked);

So saying that options 1 and 2 where checked I will end up with a byte[] array looking like this: 1100, which is expected.  I want to convert this to an integer (i.e. a bitmask) which 1100 should equal 12.

So I googled "C# byte[] to int" and most everything comes up using the BitConverter class, but I'm getting some weird values.

One example shows the checking for LittleEndian processor architecture. So:


int optionBitMask = BitConverter.ToInt32(options, 0);

And optionBitMask shows up as 16,842,752.

If I remove the reversing for LittleEndian and just use:

int optionBitMask = BitConverter.ToInt32(options, 0);

Then optionBitMask shows up as 257.

I'm not sure what I'm doing wrong.  How do I get an int value of 12 from a byte[] containing 1100 ?
2 Solutions
You should use BitArrays instead of ByteArrays

> How do I get an int value of 12 from a byte[] containing 1100 ?

using System.Collections;


BitArray ba = new BitArray(4);
            ba.Set(3, true);
            ba.Set(2, true);
            ba.Set(1, false);
            ba.Set(0, false);
            //ba= 1100

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

-> will give you 12
käµfm³d 👽Commented:
You're storing "bit" values in an entire byte for each position. You would need to treat each position as a bit rather than a byte. You can do so by working with the value of each byte and using a combination of shift and bitwise-or operations:
byte[] data = new byte[] { 1, 1, 0, 0 };
int result = 0;

for (int i = data.Length - 1; i >= 0; i--)
    result |= (data[i] << (data.Length - (i + 1)));


Open in new window

P1ST0LPETEAuthor Commented:
Thanks guys, was working on the following when I read your post:

private int BytesToInt(byte[] bytes)
        int result = 0;

        for (int i = 0; i < bytes.Length; i++)
                result += bytes[i] << i;

        return result;

So I was close, but obviously missing some crucial steps that kaufmed cleared up.  Also, had forgotten about the BitArray as there is no "bit" data type - well there's bool, but that's not exactly a bit. You know what i mean.

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now