fork download
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. uint8_t * hexstr_to_bin(
  5. char const * str) {
  6. size_t const length = strlen(str);
  7. uint8_t * result =
  8. malloc((length + 1) / 2);
  9. size_t it;
  10. for (it = 0; it < length; ++it) {
  11. char const c = str[it];
  12. uint8_t const bin =
  13. (c > '9') ?
  14. (tolower(c) - 'a' + 10) : (c - '0');
  15. if (it % 2 == 0) {
  16. result[it / 2] = bin << 4;
  17. } else {
  18. result[it / 2] |= bin;
  19. }
  20. }
  21. return result;
  22. }
  23.  
  24. int main(void) {
  25. free(hexstr_to_bin("a9"));
  26. return 0;
  27. }
Success #stdin #stdout 0s 2180KB
stdin
Standard input is empty
stdout
Standard output is empty