Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Convert byte array to bits

Convert byte array to bits

Scheduled Pinned Locked Moved C#
questioncsharpdata-structures
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Asad Hussain
    wrote on last edited by
    #1

    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

    M R 2 Replies Last reply
    0
    • A Asad Hussain

      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

      M Offline
      M Offline
      Marc Clifton
      wrote on last edited by
      #2

      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

      A 1 Reply Last reply
      0
      • M Marc Clifton

        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

        A Offline
        A Offline
        Asad Hussain
        wrote on last edited by
        #3

        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;                        
        		}
        
        M 1 Reply Last reply
        0
        • A Asad Hussain

          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

          R Offline
          R Offline
          Robert Rohde
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • A Asad Hussain

            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;                        
            		}
            
            M Offline
            M Offline
            Marc Clifton
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups