fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main()
  5. {
  6. char hex[17], bin[65] = "";
  7. int i = 0;
  8.  
  9. /* Input hexadecimal number from user */
  10. printf("Enter any hexadecimal number: ");
  11. gets(hex);
  12.  
  13. /* Extract first digit and find binary of each hex digit */
  14. for(i=0; hex[i]!='\0'; i++)
  15. {
  16. switch(hex[i])
  17. {
  18. case '0':
  19. strcat(bin, "0000");
  20. break;
  21. case '1':
  22. strcat(bin, "0001");
  23. break;
  24. case '2':
  25. strcat(bin, "0010");
  26. break;
  27. case '3':
  28. strcat(bin, "0011");
  29. break;
  30. case '4':
  31. strcat(bin, "0100");
  32. break;
  33. case '5':
  34. strcat(bin, "0101");
  35. break;
  36. case '6':
  37. strcat(bin, "0110");
  38. break;
  39. case '7':
  40. strcat(bin, "0111");
  41. break;
  42. case '8':
  43. strcat(bin, "1000");
  44. break;
  45. case '9':
  46. strcat(bin, "1001");
  47. break;
  48. case 'a':
  49. case 'A':
  50. strcat(bin, "1010");
  51. break;
  52. case 'b':
  53. case 'B':
  54. strcat(bin, "1011");
  55. break;
  56. case 'c':
  57. case 'C':
  58. strcat(bin, "1100");
  59. break;
  60. case 'd':
  61. case 'D':
  62. strcat(bin, "1101");
  63. break;
  64. case 'e':
  65. case 'E':
  66. strcat(bin, "1110");
  67. break;
  68. case 'f':
  69. case 'F':
  70. strcat(bin, "1111");
  71. break;
  72. default:
  73. printf("Invalid hexadecimal input.");
  74. }
  75. }
  76.  
  77. printf("Hexademial number = %s\n", hex);
  78. printf("Binary number = %s", bin);
  79.  
  80. return 0;
  81. }
Success #stdin #stdout 0s 4552KB
stdin
4a
stdout
Enter any hexadecimal number: Hexademial number = 4a
Binary number = 01001010