Convert from BitArray to Byte

I have an eight BitArray , I need a function to convert it to a byte. How do you do it? Specifically, I need a correct ConvertToByte function: BitArr...

I have an eight BitArray , I need a function to convert it to a byte. How do you do it?

Specifically, I need a correct ConvertToByte function:

BitArray bit = new BitArray(new bool[] { false, false, false, false, false, false, false, true }); //How to write ConvertToByte byte myByte = ConvertToByte(bit); var recoveredBit = new BitArray(new[] { myByte }); Assert.AreEqual(bit, recoveredBit);

#1 building

byte GetByte(BitArray input) { int len = input.Length; if (len > 8) len = 8; int output = 0; for (int i = 0; i < len; i++) if (input.Get(i)) output += (1 << (len - 1 - i)); //this part depends on your system (Big/Little) //output += (1 << i); //depends on system return (byte)output; }

Cheers!

#2 building

Small byte array converter: the first bit (index is "0") in BitArray is assumed to represent the least significant bit (the rightmost bit in bit eight bytes), which is interpreted as "zero" or "one" as binary.

public static class BitArrayExtender { public static byte[] ToByteArray( this BitArray bits ) { const int BYTE = 8; int length = ( bits.Count / BYTE ) + ( (bits.Count % BYTE == 0) ? 0 : 1 ); var bytes = new byte[ length ]; for ( int i = 0; i < bits.Length; i++ ) { int bitIndex = i % BYTE; int byteIndex = i / BYTE; int mask = (bits[ i ] ? 1 : 0) << bitIndex; bytes[ byteIndex ] |= (byte)mask; }//for return bytes; }//ToByteArray }//class

#3 building

In addition to the @ JonSkeet answer, you can use Generic Method as a hit:

public static byte ToByte(this BitArray bits) { if (bits.Count != 8) { throw new ArgumentException("bits"); } byte[] bytes = new byte[1]; bits.CopyTo(bytes, 0); return bytes[0]; }

And use the following:

BitArray foo = new BitArray(new bool[] { false, false, false, false,false, false, false, true }); foo.ToByte();

#4 building

It's a little late, but it works for me:

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

Apply to:

string text = "Test"; byte[] bytes = System.Text.Encoding.ASCII.GetBytes(text); BitArray bits = new BitArray(bytes); bytes[] bytesBack = BitArrayToByteArray(bits); string textBack = System.Text.Encoding.ASCII.GetString(bytesBack); // bytes == bytesBack // text = textBack

.

#5 building

Unfortunately, the BitArray class part is implemented in the. Net Core class (UWP). For example, the BitArray class cannot call the CopyTo() and Count() methods. I wrote this extension to fill in the gap:

public static IEnumerable<byte> ToBytes(this BitArray bits, bool MSB = false) { int bitCount = 7; int outByte = 0; foreach (bool bitValue in bits) { if (bitValue) outByte |= MSB ? 1 << bitCount : 1 << (7 - bitCount); if (bitCount == 0) { yield return (byte) outByte; bitCount = 8; outByte = 0; } bitCount--; } // Last partially decoded byte if (bitCount < 7) yield return (byte) outByte; }

This method uses LSB (Less Significant Byte) logic to decode BitArray into byte array. This is the same logic used by the BitArray class. Calling a method with the MSB parameter set to true will result in a sequence of bytes decoded by the MSB. In this case, remember that you may also need to reverse the final output byte set.

30 November 2019, 18:12 | Views: 5374

Add new comment

For adding a comment, please log in
or create account

0 comments