fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. const char *buf = "FE2A8D0000CA372D4F461B1D9A1883A32F018823FFFF60D30000484200000D0A0F270300030006000000B0040307000356A3";
  6.  
  7. static unsigned char hexNibble(const char *str)
  8. {
  9. unsigned char hex = *str;
  10. if (hex >= 'a' && hex <='f')
  11. {
  12. hex -= ('a' - 10);
  13. }
  14. else if (hex >= 'A' && hex <= 'F')
  15. {
  16. hex -= ('A' - 10);
  17. }
  18. else if (hex >= '0' && hex <= '9')
  19. {
  20. hex -= '0';
  21. }
  22. else
  23. {
  24. hex = 0;
  25. }
  26. return hex;
  27. }
  28.  
  29. static size_t createCharArrayFromHexString(char *result[], const char *str)
  30. {
  31. size_t len = strlen(str);
  32. if (!len) return 0;
  33. size_t bytes = len / 2;
  34. int mostSignificantNibble = len % 2;
  35. size_t size = bytes + mostSignificantNibble;
  36. *result = malloc(size);
  37. char *out = *result;
  38. const char *in = str;
  39. if (mostSignificantNibble)
  40. {
  41. *out++ = hexNibble(in++);
  42. }
  43. while (bytes)
  44. {
  45. *out = hexNibble(in++);
  46. *out <<= 4;
  47. *out++ |= hexNibble(in++);
  48. --bytes;
  49. }
  50. return size;
  51. }
  52.  
  53. int main(void) {
  54. int value = 0;
  55. char *converted;
  56.  
  57. size_t convertedSize = createCharArrayFromHexString(&converted, buf);
  58.  
  59. for (int i = 0; i < convertedSize; ++i)
  60. {
  61. printf( "0x%02hhx\n", converted[i]);
  62. }
  63.  
  64. free(converted);
  65. }
  66.  
Success #stdin #stdout 0s 10320KB
stdin
Standard input is empty
stdout
0xfe
0x2a
0x8d
0x00
0x00
0xca
0x37
0x2d
0x4f
0x46
0x1b
0x1d
0x9a
0x18
0x83
0xa3
0x2f
0x01
0x88
0x23
0xff
0xff
0x60
0xd3
0x00
0x00
0x48
0x42
0x00
0x00
0x0d
0x0a
0x0f
0x27
0x03
0x00
0x03
0x00
0x06
0x00
0x00
0x00
0xb0
0x04
0x03
0x07
0x00
0x03
0x56
0xa3