0

I have already wrote a program to this , bits working only if bit array length is multiples of 8 . Can some one help me to get bit array of 5 bits converted into byte

both functions work only when it have bit array in multiple of 8 .

public static byte[] BitArrayToByteArray(BitArray bits)
        {
            byte[] ret = new byte[bits.Length / 8];
            bits.CopyTo(ret, 0);
            return ret;
        }


public static byte[] ToByteArray(this BitArray bits)
        {
            int numBytes = bits.Count / 8;
            if (bits.Count % 8 != 0) numBytes++;

            byte[] bytes = new byte[numBytes];
            int byteIndex = 0, bitIndex = 0;

            for (int i = 0; i < bits.Count; i++)
            {
                if (bits[i])
                    bytes[byteIndex] |= (byte)(1 << (7 - bitIndex));

                bitIndex++;
                if (bitIndex == 8)
                {
                    bitIndex = 0;
                    byteIndex++;
                }
            }
            return bytes;
        }
Sagar
  • 3
  • 3
  • Be aware of that: The first byte in the array represents bits 0 through 7, the second byte represents bits 8 through 15, and so on. The Least Significant Bit of each byte represents the lowest index value: " bytes [0] & 1" represents bit 0, " bytes [0] & 2" represents bit 1, " bytes [0] & 4" represents bit 2, and so on. (from: http://msdn.microsoft.com/en-us/library/x1xda43a.aspx) – qqbenq Mar 28 '14 at 17:55

1 Answers1

2

Basically you need to round up the number of bytes needed in your first method:

byte[] ret = new byte[(bits.Length + 7) / 8];
bits.CopyTo(ret, 0);

Your second method already looks okay at first glance... it certainly fills the right number of bytes. It may not fill them in the way you want it to, but in that case you need to provide more detail about how you expect it to be filled. You may want to just change the initial value of bitIndex for example. (Sample input and output would be immensely useful.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    I think that (with appropriate casts) `Math.Ceiling(bits.Length / 8d)` would be equivalent (IMO clearer intent, except for the casts) – Tim S. Mar 28 '14 at 17:55
  • @TimS.: Possibly - this is the way I tend to do it, as I then don't need to think about any details. It's easy enough to extract this as an extension method if you do it regularly, of course. – Jon Skeet Mar 28 '14 at 17:56
  • @TimS.: Binary or decimal, conversion to floating point is expensive and so is floating point division. For such a trivial operation, doing all that just so you can use `ceil()` is...overkill. Integer division expresses the intent just as well (bettin, IMHO): `bits.Length / 8 + (bits.Length % 8 ? 1 : 0 )`. Since your audience here are bit-twiddlers, and you're dealing with a power of 2, avoid the division operation entirely. Speak your audience's language and bit-twiddle your way to the required number of bytes: `bits.Length>>3 + ((bits.Length&7) == 0 ? 0 : 1 )`. – Nicholas Carey Mar 28 '14 at 18:43