C
C#3y ago
Matt

Condense Byte[] to BitArray to smaller Byte[]

I have an array of 300 bytes, all either 0 or 1. I need a function that takes in the byte array and performs a bitwise operation to convert the bytes to bits. As an example: If the first 8 bytes of the input array are
{ 01, 01, 00, 00, 00, 00, 00, 00 }
{ 01, 01, 00, 00, 00, 00, 00, 00 }
Then the BitArray should end up as
{ 00000011 }
{ 00000011 }
Which ends up as a byte 03 Obviously the array will have to be handled in chunks of 8. The resulting byte array should be about 40 bytes long, hence condensing the 300 byte array to a 40 bytes array. I can't deviate from this structure unfortunately, I wish I could but this is what is required. Any advice on anything I should read up on or a fancy linq statement that will do this all for me would be greatly appreciated. Thank you
6 Replies
TheBoxyBear
TheBoxyBear3y ago
Loop up bit manipulation to get the bits you need in the second array Mainly | and &
Matt
MattOP3y ago
okay I'll start researching. Thanks
TheBoxyBear
TheBoxyBear3y ago
BitArray has a constructor that takes an array of ints so as long as your set is a multiple of 32 bits then you could use that to be efficient, combinining the bytes into ints << and >> are also useful for that ... It also has a constructor that takes an array of bytes so you could just cheese the whole thing
Matt
MattOP3y ago
amazing. This I will definitely look into lol okay I mean it's kinda useful that I was already working with ints. Might switch to bytes to make it easier. We'll see
TheBoxyBear
TheBoxyBear3y ago
Useful to learn bit manipulation while you're at it even if you don't end up using it
Matt
MattOP3y ago
yes. The first element in the input byte array should be linked to the 1 bit (binary representation) in the BitArray then the second to the 2 bit. The third to the 4 bit. etc but I could just reverse each chunk of my byte array no?
byte[] inputBytes = new byte[16]
{ 00, 01, 01, 01, 01, 01, 00, 00, 00, 00, 01, 01, 00, 01, 00, 01 };
byte[] outputBytes = new byte[inputBytes.Length / 8];
for(int i = 0; i < (inputBytes.Length / 8); i++)
{
BitArray bits = new BitArray(inputBytes
.Skip(i * 8)
.Take(8)
.Reverse()
.ToArray());
bits.CopyTo(outputBytes, i);
}
Console.WriteLine("{0:X}", outputBytes[0]);
byte[] inputBytes = new byte[16]
{ 00, 01, 01, 01, 01, 01, 00, 00, 00, 00, 01, 01, 00, 01, 00, 01 };
byte[] outputBytes = new byte[inputBytes.Length / 8];
for(int i = 0; i < (inputBytes.Length / 8); i++)
{
BitArray bits = new BitArray(inputBytes
.Skip(i * 8)
.Take(8)
.Reverse()
.ToArray());
bits.CopyTo(outputBytes, i);
}
Console.WriteLine("{0:X}", outputBytes[0]);
built this as a test but it fails as index out of bounds on bits.CopyTo(outputBytes, i) it's because feeding the BitArray a byte array it takes in all of the bits for the bytes not sure how to convert from a byte of 00 to a bit of 0 and a byte of 01 to a bit of 1
byte[] inputBytes = new byte[16]
{ 01, 01, 01, 00, 00, 00, 00, 00,
01, 01, 01, 01, 01, 01, 01, 01 };
byte[] outputBytes = new byte[inputBytes.Length / 8];
for(int i = 0; i < (inputBytes.Length / 8); i++)
{
byte[] b = inputBytes.Skip(i * 8).Take(8).ToArray();
BitArray bits = new BitArray(8);
for (int j = 0; j < 8; j++)
{
if (b[j] == 1)
{
bits[j] = true;
}
}
bits.CopyTo(outputBytes, i);
}
Console.WriteLine("{0:X}", outputBytes[0]);
Console.WriteLine("{0:X}", outputBytes[1]);
Console.ReadLine();
byte[] inputBytes = new byte[16]
{ 01, 01, 01, 00, 00, 00, 00, 00,
01, 01, 01, 01, 01, 01, 01, 01 };
byte[] outputBytes = new byte[inputBytes.Length / 8];
for(int i = 0; i < (inputBytes.Length / 8); i++)
{
byte[] b = inputBytes.Skip(i * 8).Take(8).ToArray();
BitArray bits = new BitArray(8);
for (int j = 0; j < 8; j++)
{
if (b[j] == 1)
{
bits[j] = true;
}
}
bits.CopyTo(outputBytes, i);
}
Console.WriteLine("{0:X}", outputBytes[0]);
Console.WriteLine("{0:X}", outputBytes[1]);
Console.ReadLine();
This works but is horribly inefficient I imagine. Any ideas?
Want results from more Discord servers?
Add your server