Advertisement
Guest User

Untitled

a guest
May 12th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char GetBinary(char hex[]);
  6.  
  7. int main(int argc, char** argv) {
  8.     printf("%s", GetBinary("560a"));
  9. }
  10.  
  11. char GetBinary(char hex[]) {
  12.     char bin[100] = "";
  13.     int i = 0;
  14.     for (i = 0; hex[i] != '\0'; i++) {
  15.         switch (hex[i]) {
  16.             case '0':
  17.                 strcat(bin, "0000");
  18.                 break;
  19.             case '1':
  20.                 strcat(bin, "0001");
  21.                 break;
  22.             case '2':
  23.                 strcat(bin, "0010");
  24.                 break;
  25.             case '3':
  26.                 strcat(bin, "0011");
  27.                 break;
  28.             case '4':
  29.                 strcat(bin, "0100");
  30.                 break;
  31.             case '5':
  32.                 strcat(bin, "0101");
  33.                 break;
  34.             case '6':
  35.                 strcat(bin, "0110");
  36.                 break;
  37.             case '7':
  38.                 strcat(bin, "0111");
  39.                 break;
  40.             case '8':
  41.                 strcat(bin, "1000");
  42.                 break;
  43.             case '9':
  44.                 strcat(bin, "1001");
  45.                 break;
  46.             case 'a':
  47.             case 'A':
  48.                 strcat(bin, "1010");
  49.                 break;
  50.             case 'b':
  51.             case 'B':
  52.                 strcat(bin, "1011");
  53.                 break;
  54.             case 'c':
  55.             case 'C':
  56.                 strcat(bin, "1100");
  57.                 break;
  58.             case 'd':
  59.             case 'D':
  60.                 strcat(bin, "1101");
  61.                 break;
  62.             case 'e':
  63.             case 'E':
  64.                 strcat(bin, "1110");
  65.                 break;
  66.             case 'f':
  67.             case 'F':
  68.                 strcat(bin, "1111");
  69.                 break;
  70.         }
  71.     }
  72.     return(bin);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement