Advertisement
jiebing

Untitled

Sep 18th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1. #define BUFLEN 16
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. int nibble_to_int(char nibble) {
  7.     if ('0' <= nibble && nibble <= '9') return nibble - '0';
  8.     else return nibble - 'a' + 10;
  9. }
  10.  
  11. void dehexify() {
  12.     struct {
  13.         char answer[BUFLEN];
  14.         char buffer[BUFLEN];
  15.     } c;
  16.     int i = 0, j = 0;
  17.  
  18.     gets(c.buffer);
  19.  
  20.     while (c.buffer[i]) {
  21.         if (c.buffer[i] == '\\' && c.buffer[i+1] == 'x') {
  22.             int top_half = nibble_to_int(c.buffer[i+2]);
  23.             int bottom_half = nibble_to_int(c.buffer[i+3]);
  24.             c.answer[j] = top_half << 4 | bottom_half;
  25.             i += 3;
  26.         } else {
  27.             c.answer[j] = c.buffer[i];
  28.         }
  29.         i++; j++;
  30.     }
  31.  
  32.     c.answer[j] = 0;
  33.     printf("%s\n", c.answer);
  34.     fflush(stdout);
  35. }
  36.  
  37. int main() {
  38.     while (!feof(stdin)) {
  39.         dehexify();
  40.     }
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement