Convert byte array to bits
-
I have an array of bytes which i need to get bits from in 5-bit chunks. e.g. lets say my byte array has 4 values 0 - 10101010 1 - 10101010 2 - 10101010 3 - 10101010 i would like an array of strings (or whatever, could be bytes with leading 0) that returns to me the values 0 - (000)10101 (first 5 bits of 0) 1 - (000)01010 (last 3 bits of 0 and first 2 bits of 1) 2 - (000)10101 (mid 5 bits of 1) . . so on How can I do this in C#? Thanks
-
I have an array of bytes which i need to get bits from in 5-bit chunks. e.g. lets say my byte array has 4 values 0 - 10101010 1 - 10101010 2 - 10101010 3 - 10101010 i would like an array of strings (or whatever, could be bytes with leading 0) that returns to me the values 0 - (000)10101 (first 5 bits of 0) 1 - (000)01010 (last 3 bits of 0 and first 2 bits of 1) 2 - (000)10101 (mid 5 bits of 1) . . so on How can I do this in C#? Thanks
Since that's only 32 combinations, why not use a lookup table? Then all you need to do is some bit shifting and and'ing. Marc MyXaml Advanced Unit Testing YAPO
-
Since that's only 32 combinations, why not use a lookup table? Then all you need to do is some bit shifting and and'ing. Marc MyXaml Advanced Unit Testing YAPO
Thanks Marc, this seems to do the trick but if any1 has a suggestion to make this process faster and less complicated then go right ahead.
private byte[ ] EncodeArray( byte[ ] parentArray ) { byte[ ] retArr = new byte[ parentArray.Length * 8 / 5 ]; int i = 0; int pos = 0; while ( i < parentArray.Length ) { if ( i % 5 == 0 ) { byte mask = 0xF8; //11111000 int result = parentArray[ i ] & mask ; retArr[ pos++ ] = ( byte ) result; mask = 0x7; //111 byte mask2 = 0xC0; //11000000 int result1 = parentArray[ i ] & mask ; int result2; if ( i + 1 < parentArray.Length ) { result2 = parentArray[ i + 1 ] & mask2 ; } else result2 = 0; result = ( result1 << 2 ) + ( result2 >> 6); retArr[ pos++ ] = ( byte ) result; } else if ( i % 5 == 1 ) { byte mask = 0x3E; //111110 int result = parentArray[ i ] & mask; retArr[ pos++ ] = ( byte ) result; mask = 0x1; // 1 byte mask2 = 0xF0; //11110000 int result1 = parentArray[ i ] & mask; int result2; if ( i + 1 < parentArray.Length ) { result2 = parentArray[ i + 1 ] & mask2 ; } else result2 = 0; result = ( result1 << 4 ) + ( result2 >> 4 ); retArr[ pos++ ] = ( byte ) result; } else if ( i % 5 == 2 ) { byte mask = 0xF; //00001111 byte mask2 = 0x80; // 10000000 int result1 = parentArray[ i ] & mask; int result2; if ( i + 1 < parentArray.Length ) { result2 = parentArray[ i + 1] & mask2; } else result2 = 0; int result = ( result1 << 1 ) + ( result2 >> 7 ); retArr[ pos++ ] = ( byte ) result; } else if ( i % 5 == 3 ) { byte mask = 0x7C; //1111100 int result = parentArray [ i ] & mask; retArr[ pos++ ] = ( byte ) result; mask = 0x3; //11 byte mask2 = 0xE0; //11100000 int result1 = parentArray[ i ] & mask; int result2; if ( i + 1 < parentArray.Length ) { result2 = parentArray[ i + 1] & mask2; } else result2 = 0; result = ( result1 << 2 ) + ( result2 >> 5 ); retArr[ pos++ ] = ( byte ) result; } else if ( i % 5 == 3 ) { byte mask = 0x1F; //11111 int result = parentArray [ i ] & mask; retArr[ pos++ ] = ( byte ) result; } i++; } return retArr; }
-
I have an array of bytes which i need to get bits from in 5-bit chunks. e.g. lets say my byte array has 4 values 0 - 10101010 1 - 10101010 2 - 10101010 3 - 10101010 i would like an array of strings (or whatever, could be bytes with leading 0) that returns to me the values 0 - (000)10101 (first 5 bits of 0) 1 - (000)01010 (last 3 bits of 0 and first 2 bits of 1) 2 - (000)10101 (mid 5 bits of 1) . . so on How can I do this in C#? Thanks
Have a look at the
BitArray
class. You can feed it with a byte array and afterwards pick the bits one after the other:BitArray ba = new BitArray(b);
int counter = 0;
string temp = "";
while (counter < ba.Length)
{
int mod = counter % 8;
temp += ba[counter - 2 * mod + 7] ? "1" : "0";
if (counter % 5 == 4)
{
Console.WriteLine(temp);
temp = "";
}
counter++;
}
if (temp.Length > 0)
Console.WriteLine("Left: " + temp);The formula when accessing the BitArray indexer seems a bit complicated and results from the fact that the bits from each byte put into the constructor seem to be reversed.
-
Thanks Marc, this seems to do the trick but if any1 has a suggestion to make this process faster and less complicated then go right ahead.
private byte[ ] EncodeArray( byte[ ] parentArray ) { byte[ ] retArr = new byte[ parentArray.Length * 8 / 5 ]; int i = 0; int pos = 0; while ( i < parentArray.Length ) { if ( i % 5 == 0 ) { byte mask = 0xF8; //11111000 int result = parentArray[ i ] & mask ; retArr[ pos++ ] = ( byte ) result; mask = 0x7; //111 byte mask2 = 0xC0; //11000000 int result1 = parentArray[ i ] & mask ; int result2; if ( i + 1 < parentArray.Length ) { result2 = parentArray[ i + 1 ] & mask2 ; } else result2 = 0; result = ( result1 << 2 ) + ( result2 >> 6); retArr[ pos++ ] = ( byte ) result; } else if ( i % 5 == 1 ) { byte mask = 0x3E; //111110 int result = parentArray[ i ] & mask; retArr[ pos++ ] = ( byte ) result; mask = 0x1; // 1 byte mask2 = 0xF0; //11110000 int result1 = parentArray[ i ] & mask; int result2; if ( i + 1 < parentArray.Length ) { result2 = parentArray[ i + 1 ] & mask2 ; } else result2 = 0; result = ( result1 << 4 ) + ( result2 >> 4 ); retArr[ pos++ ] = ( byte ) result; } else if ( i % 5 == 2 ) { byte mask = 0xF; //00001111 byte mask2 = 0x80; // 10000000 int result1 = parentArray[ i ] & mask; int result2; if ( i + 1 < parentArray.Length ) { result2 = parentArray[ i + 1] & mask2; } else result2 = 0; int result = ( result1 << 1 ) + ( result2 >> 7 ); retArr[ pos++ ] = ( byte ) result; } else if ( i % 5 == 3 ) { byte mask = 0x7C; //1111100 int result = parentArray [ i ] & mask; retArr[ pos++ ] = ( byte ) result; mask = 0x3; //11 byte mask2 = 0xE0; //11100000 int result1 = parentArray[ i ] & mask; int result2; if ( i + 1 < parentArray.Length ) { result2 = parentArray[ i + 1] & mask2; } else result2 = 0; result = ( result1 << 2 ) + ( result2 >> 5 ); retArr[ pos++ ] = ( byte ) result; } else if ( i % 5 == 3 ) { byte mask = 0x1F; //11111 int result = parentArray [ i ] & mask; retArr[ pos++ ] = ( byte ) result; } i++; } return retArr; }
This seems overly complex and there are some errors. It seems that the masks should go: F8, 7C, 3E, 1F Also, there's two tests for (i % 5) == 3 Instead of changing the mask, why not shift the byte under test and 'or' in the necessary bits from a look ahead byte (which is 0 if at the end of the array). It's a bit complicated (no pun intended) because you have to figure out when to get the next byte, but you should be able to do the whole thing in a loop. Marc MyXaml Advanced Unit Testing YAPO