Bit Array To Byte Array

image_pdfimage_print
   
 
//http://isotopescreencapture.codeplex.com/
//The MIT License (MIT)
namespace Isotope.Collections
{
    public static class BitArrayUtil
    {
        public static byte[] BitArrayToBytes(System.Collections.BitArray bitarray)
        {
            if (bitarray.Length == 0)
            {
                throw new System.ArgumentException("must have at least length 1", "bitarray");
            }

            int num_bytes = bitarray.Length / 8;
            
            if (bitarray.Length % 8 != 0)
            {
                num_bytes += 1;
            }

            var bytes = new byte[num_bytes];
            bitarray.CopyTo(bytes, 0);
            return bytes;
        }
    }
}

   
     


This entry was posted in File Stream. Bookmark the permalink.