fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void to_binary(int d) {
  6. int x = d, ind = 7;
  7. char *buf = (char*) malloc(sizeof(char) * 8);
  8. memset(buf, '0', 8);
  9.  
  10. while (x > 0) {
  11. int mod_val = x % 2;
  12. x >>= 1;
  13. buf[ind--] = (int) '0' + mod_val;
  14. //printf("%c ", buf[ind + 1]);
  15. }
  16. for (int i = 0; i <= 7; i++) printf("%c", buf[i]);
  17. printf(" ");
  18. free(buf);
  19. buf = NULL;
  20. }
  21.  
  22. int main(void) {
  23. // your code goes here
  24. char input[50], ch;
  25. int i = 0, j = 0;
  26.  
  27. printf("Enter Cleartext: ");
  28. while(1) {
  29. ch = getchar();
  30. if (ch == '\n' || ch == EOF) break;
  31.  
  32. input[j++] = ch;
  33. }
  34. input[j] = '\0';
  35.  
  36. printf("\nHex encoding is: \n");
  37. for(i = 0; input[i] != '\0'; i++) {
  38. printf("%x ", input[i]);
  39. }
  40. printf("\nBinary encoding is: \n");
  41. for(i = 0; input[i] != '\0'; i++) {
  42. to_binary(input[i]);
  43. }
  44.  
  45.  
  46.  
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0s 4516KB
stdin
Hello
stdout
Enter Cleartext: 
Hex encoding is: 
48 65 6c 6c 6f 
Binary encoding is: 
01001000 01100101 01101100 01101100 01101111