fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main(void) {
  5. char text[] = "thank you";
  6. int len = strlen(text);
  7.  
  8. char hex[100], string[50];
  9.  
  10. // Convert text to hex.
  11. int i,j;
  12.  
  13. for ( i = 0, j = 0; i < len; i++, j+= 5) {
  14. sprintf(hex + j, "0x%02X ", text[i] );
  15. printf("0x%X ", text[i] );
  16. }
  17. printf("'%s' in hex is %s.\n", text, hex); //'thank you' in hex is 7468616e6b20796f75.
  18.  
  19. // Convert the hex back to a string.
  20. len = strlen(hex);
  21. for (i = 0, j = 2; j < len; i++, j+= 5) {
  22. int val;
  23. sscanf(hex + j, "%2x", &val);
  24. string[i] = val;
  25. }
  26. string[i + 1] = '\0';
  27. printf("%s as a string is '%s'.\n", hex, string);
  28.  
  29. return 0;
  30. }
  31.  
Success #stdin #stdout 0s 4460KB
stdin
Standard input is empty
stdout
0x74 0x68 0x61 0x6E 0x6B 0x20 0x79 0x6F 0x75 'thank you' in hex is 0x74 0x68 0x61 0x6E 0x6B 0x20 0x79 0x6F 0x75 .
0x74 0x68 0x61 0x6E 0x6B 0x20 0x79 0x6F 0x75  as a string is 'thank you'.