Advertisement
jzh4n

Binary Decoder

Dec 16th, 2017
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main() {
  5.     char str[100], output[100], buffer;
  6.     int len, len2;
  7.  
  8.     gets(str);
  9.    
  10.     len = strlen(str);
  11.  
  12.     len2 = 0;
  13.     buffer = 0;
  14.     for(int i = len - 1, shift = 0; i >= 0; --i) {
  15.         if(str[i] == '.') {
  16.             shift = 0;
  17.             output[len2++] = buffer;
  18.             buffer = 0;
  19.  
  20.             continue;
  21.         }
  22.  
  23.         if(str[i] == '1')
  24.             buffer += (1 << shift);
  25.  
  26.         shift++;
  27.     }
  28.  
  29.     output[len2++] = buffer;
  30.     output[len2] = '\0';
  31.  
  32.     for(int i = len2 - 1; i >= 0; --i)
  33.         printf("%c", output[i]);
  34.  
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement