Advertisement
grosul

Untitled

Dec 9th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.84 KB | None | 0 0
  1. char* convert(const char* str) {
  2.   char* end = str + strlen(str) - 1;
  3.   char* result = malloc(sizeof(str));
  4.   size_t result_id = 0;
  5.   size_t current_symbol = 0;
  6.   size_t pow_2 = 1;
  7.   while (end >= str) {
  8.     if (*end == '1') {
  9.       current_symbol += pow_2;
  10.     }
  11.  
  12.     if (pow_2 == 8 || end == str) {
  13.       pow_2 = 1;
  14.       if (current_symbol <= 9) {
  15.         result[result_id++] = '0' + current_symbol;
  16.       } else {
  17.         result[result_id++] = 'A' + current_symbol - 10;
  18.       }
  19.       current_symbol = 0;
  20.     } else {
  21.       pow_2 *= 2;
  22.     }
  23.  
  24.     --end;
  25.   }
  26.   result[result_id--] = '\0';
  27.   for (size_t i = 0; i < result_id; ++i) {
  28.     if (i < result_id - i) {
  29.       char tmp = result[i];
  30.       result[i] = result[result_id - i];
  31.       result[result_id - i] = tmp;
  32.     } else {
  33.       break;
  34.     }
  35.   }
  36.  
  37.   return result;
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement