logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/

bytes2hex.c (590B)


  1. // utils-std: Collection of commonly available Unix tools
  2. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: 0BSD
  4. #define _POSIX_C_SOURCE 200809L
  5. #include "./strconv.h"
  6. #include <assert.h>
  7. void
  8. bytes2hex(const uint8_t *data, size_t datalen, char *buf, size_t buflen)
  9. {
  10. static const char *hextab = "0123456789abcdef";
  11. assert(buflen >= datalen * 2);
  12. size_t bi = 0;
  13. for(size_t i = 0; i < datalen; i++)
  14. {
  15. buf[bi++] = hextab[data[i] / 0x10];
  16. buf[bi++] = hextab[data[i] % 0x10];
  17. }
  18. if(bi < buflen) buf[bi] = '\0';
  19. }