utils.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * utils.c
  3. *
  4. * Copyright (C) 2019 Sylvain Munaut
  5. * All rights reserved.
  6. *
  7. * LGPL v3+, see LICENSE.lgpl3
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. */
  23. #include <stdint.h>
  24. #include <stdbool.h>
  25. char *
  26. hexstr(void *d, int n, bool space)
  27. {
  28. static const char * const hex = "0123456789abcdef";
  29. static char buf[96];
  30. uint8_t *p = d;
  31. char *s = buf;
  32. char c;
  33. while (n--) {
  34. c = *p++;
  35. *s++ = hex[c >> 4];
  36. *s++ = hex[c & 0xf];
  37. if (space)
  38. *s++ = ' ';
  39. }
  40. s[space?-1:0] = '\0';
  41. return buf;
  42. }