I need to convert several decimal numbers (or a string or 1 and 0) to a binary combination. In .NET I see many libraries working with Byte. A Byte is a manipulation of 8 bits. In my case I have to work with set of 3 bit that I need to concatenate together.
For example:
for Filter or Partition I have a 3 position bit.
What should I use to help me for this kind of conversion? At this moment I try to understand BitArray but I don't understand how to create bit a specific size then fill them easily.
Here is what I already did
BitArray headerBits = new BitArray(new bool[] { false, false, true, true, false, false, false, false }); // 8
BitArray filterBits = new BitArray(new bool[] { false, true, true }); // 11
BitArray PartitionBits = new BitArray(new bool[] { true, false, true }); // 14
BitArray CompanyPrefixBits = new BitArray(new bool[] { false, false }); // 16
Let's try with these 16 first bits. The result I want is
3074
EDIT ------
BitArray headerBits = new BitArray(new bool[] { false, false, true, true, false, false, false, false }); // 8
BitArray filterBits = new BitArray(new bool[] { false, true, true }); // 11
BitArray PartitionBits = new BitArray(new bool[] { true, false, true }); // 14
BitArray CompanyPrefixBits = new BitArray(new bool[] { false, false }); // 16
BitArray newBitArray = new BitArray(headerBits.Cast<bool>()
.Concat(filterBits.Cast<bool>())
.Concat(PartitionBits.Cast<bool>())
.Concat(CompanyPrefixBits.Cast<bool>())
.ToArray());
var byteArray = newBitArray.ToByteArray();
Console.WriteLine($"{BitConverter.ToString(byteArray, 0)}");
// Result is 0C-2E
// I expect 30-74
How is this possible?