bazz

binascii to bin converter (little-endian)

Jun 18th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.16 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /*
  4.  
  5. little-endian ascii-bin to bin converter!
  6. Anything NOT ascii-bin ('1' or '0') is skipped
  7.  
  8. authored (in 10 minutes) (unconventionally) in 2015 by: bazz | http://www.bazz1.com
  9. */
  10.  
  11. char message[] = "00000000111111111101100011111111000000000000111000000000000000010000000000000000010010100100011000000000010010010100011000000000000000000000000000010000001000000000000000000000000000000110010000000000000000000110010000000000000000000000000000001111111111100000000000001100000000000001000100000000010001000111010101100011000000000000011010110111100100000000000000000000000000010000000000000000000001000000000000000000000000000000000000000110010000000000000000000000000000001111111100000000111000100000110001011000000000000000010010010100001101000000000000000011010111110101000000000000010100100100111101000110000000000000010010010100110001000000000000000101000000000000000100000000000000010000000000000000000000000000000011000100100001000000000000001100011010010110111000000000011011110000001000010000000000000000000000000000000001100000000000001101011011100111010000000000011100100101001001000111000000000000010000100010000001010000000000001000010110010101101000000000001000000000011111001110000000000000000000000000001000000000000000000000000010010000000000000000000001100000000000110001000000000000000000000000000001100000000000000001011000110111001100000000011100000100110101010011000000000000010001100101010000000000000000000000000000000000000000000000000000000100100101000101000000000000010000110010000001110000000000000011010100100100011100000000010000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111011000000000110101100000000000000001000000000000000000000000000000000000000000000000000000001101001100000000001011010100100001010000000000000000001000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000101100000000000000011011100000111001000000000011101000000000000000000000000000000000000010101000000000000000000000000000000000000000000000000001100110110010001100101000000000000011100110110001100000000000000000000000000000000000100000000100001000000000000000000000000000000000000000110110001110000000000000111011101000111000000000000011101000000000000000000000000000000000000011111000000000000000000000000000000000000000000000000000101000110001001101011000000000000011100000111010000000000000000000000000000000000001000000000000001000000000000000000000000000000000000000001010001110000000000000010010110000101100100000000010110100000000000000000000000000000000000100001100000000000000000000000000000000000000000000000000101000110011101011000000000000000010110010101101000000000000000000000000000000000001000000000001011000000000000000000000000000000000000000001010001100000000000000010010110000101100100000000010110100000000000000000000000000000000000100100000000000000000000000000000000000000000000000000000101000110010001101101000000000000011011100110010000000000000000000000000000000000001000000000010101000000000000000000000000000000000000000111000001100000000000000100011011010110010000000000011001000000000000000000000000000000000000101100010000000000000000000000000000000000000000000000100010000111011001110101000000000000011001010110010000000000000000000000000000000000001100000000010011000000000000000000000000000000000000001000011001110000000000000110011010010110010100000000011101110000000000000000000000000000000000111101010000000000000000000000000000000000000000000000001001000110110001110101000000000000011011010110100100000000000000000000000000000000001100000000111110000000000000000000000000000000000000000001010001100000000000001101011001010110000100000000011100110000000000000000000000000000000001000000110000000000000000000000000000000000000000000000001001000111010001100101000000000000011000110110100000000000000000000000000000000000010000000000001100000000000000000000000000000000000000000000110001110000000000000010010101000101001000000000010000110000000000000000000000000000000001000011110000000000000000000000000000000000100000000000000011000110011101010100000000000000010100100100001100000000000000000000000000000000010000000000001111000000000000000000000000000000000010000000110001100000000000000010010101000101001000000000010000110000000000000000000000000000000001000011110000000000000000000000000000000000100000000000000011000111010001100101000000000000011110000111010000000000000000000000000000000000000000000000000000000100001101101111000000000000011100000111100101110000000000000010011010010110011100000000011010000111010000100000000000000000001010000110001100100000000000001001001000000011000100000000001110010011100100111000000000000000001000000100100001100000000000000101011101110110110000000000011001010111010001110100000000000000001011010101000001100000000000000001011000110110101100000000011000010111001001100100000000000000001000000100001101100000000000001111011011010111000000000000011000010110111001111001000000000000000000000000000001100000000000000100011001010111001100000000011000110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100100111001101010010000000000000010001110100001000100000000000000000010010010100010100000000010000110011011000110001000000000000001110010011011000110000000000000110001011010011001000000000001011100011000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000010011100110101001000000000010001110100001000100000000000000000010010010100010101000000000000000011001101100011000100000000001110010011011000110110000000000000001011010011001000100000000000001110001100010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101100001011001000000000000010110100010000000000000000000000000000000000000000000000000000000000000000000000000000000000000111100110101000100000000000000000000000000010000000000000000000000000000000000000001000000000000000101101100110001010000000000001000010110010101101000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101100001011001000000000000010110100010000000000000000000000000000000000000000000000000000000000000000000000000000000000000011011111010001000000000000000000000000000000011100000000000111101010000000000000000000000000000000000111001000001010000000000001000010110010101101000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000110001000000000100110010000000000000000000000000000101101111000010100000000000000000000000000000001100000000000110110100101100001011001000000000000010110100010000000000000000000000000000000000000000000000000000000000000000000000000000000000000001001001010000000000000000000000000000000000000111100000000100001000000000000000000000000000000101101101100111101100000000000000100011001010111001100000000011000110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101100100100101000101";
  12. //char message[] = "00000001 00000010 11111111"; // ff0201 test
  13. char *lut[] =
  14. {
  15.     "0000", // 0
  16.     "0001", // 1
  17.     "0010", // 2
  18.     "0011", // 3
  19.     "0100", // 4
  20.     "0101", // 5
  21.     "0110", // 6
  22.     "0111", // 7
  23.     "1000", // 8
  24.     "1001", // 9
  25.     "1010", // 10
  26.     "1011", // 11
  27.     "1100", // 12
  28.     "1101", // 13
  29.     "1110", // 14
  30.     "1111"  // 15
  31. };
  32.  
  33. int main()
  34. {
  35.     int x;
  36.  
  37.     // one byte at a time
  38.     for (x=0; x < strlen(message); x)
  39.     {
  40.         // skip spaces or anything that's not binary ascii!
  41.         if (message[x] != '1' && message[x] != '0')
  42.         {
  43.             x++; continue;
  44.         }
  45.  
  46.         char final_byte=0;
  47.         int i=0;
  48.         char lo[4+1];
  49.         char hi[4+1];
  50.         // read 4
  51.         hi[i++] = message[x++];
  52.         hi[i++] = message[x++];
  53.         hi[i++] = message[x++];
  54.         hi[i++] = message[x++];
  55.         hi[i] = 0;
  56.  
  57.         i=0;
  58.         lo[i++] = message[x++];
  59.         lo[i++] = message[x++];
  60.         lo[i++] = message[x++];
  61.         lo[i++] = message[x++];
  62.         lo[i] = 0;
  63.  
  64.         for (i=0; i < 16; i++)
  65.         {
  66.             if (!strcmp(lo, lut[i]))
  67.             {
  68.                 final_byte = i;
  69.             }
  70.         }
  71.         for (i=0; i < 16; i++)
  72.         {
  73.             if (!strcmp(hi, lut[i]))
  74.             {
  75.                 final_byte |= (i<<4);
  76.             }
  77.         }
  78.         printf("%c", final_byte);
  79.     }
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment