Reputation: 18485
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?
Upvotes: 1
Views: 179
Reputation: 9209
I see that you're reading data from an RFID tag. All data can be treated as a series of bytes, hence the examples using bytes.
If the data is to be displayed as a string in hexadecimal format then I would suggest that you read the data into an array and then use
System.Convert.ToString(data, base);
where data is your 16 bit value and base can be 2,8, 10, 16 for binary, octal, decimal or hexadecimal.
Upvotes: 0
Reputation: 18023
I try to understand BitArray but I don't understand how to create bit a specific size then fill them easily.
BitArray
has several constructors. The most obvious is to create the BitArray
from a bool
array:
var bits = new BitArray(new[] { true, false, true, true });
The other approach is to create an uninitialized BitArray
just by the size constructor and then setting the bits one by one:
var bits = new BitArray(4); // 4 bits, all false for now
for (int i = 0; i < bits.Length; i++)
bits[i] = GetMyNextBit();
Update:
If your main issue is to convert the concatenated bits into bytes you had better you use one big BitArray
for all the fields.
After you set all of the bits you can use this extension method:
public static byte[] ToByteArray(this BitArray bits)
{
byte[] result = new byte[(bits.Length - 1) / 8 + 1];
bits.CopyTo(result, 0);
return result;
}
i'm only interested by hexadecimal
You can easily convert the bytes into hex string:
var result = new StringBuilder(bytes.Length * 2);
for (int i = 0; i < bytes.Length; i++)
result.Append(bytes[i].ToString("X2"));
Upvotes: 1