test_mbedtls_utils.c 705 B

123456789101112131415161718192021222324252627282930
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <stdint.h>
  8. #include "test_mbedtls_utils.h"
  9. static inline char to_hex_digit(unsigned val)
  10. {
  11. return (val < 10) ? ('0' + val) : ('a' + val - 10);
  12. }
  13. void utils_bin2hex(char *const hex, const size_t hex_maxlen, const unsigned char *const bin, const size_t bin_len)
  14. {
  15. assert(bin_len < SIZE_MAX / 2);
  16. assert(hex_maxlen > bin_len * 2U);
  17. assert(hex);
  18. assert(bin);
  19. int i;
  20. for (i = 0; i < bin_len; i++) {
  21. hex[2*i] = to_hex_digit(bin[i] >> 4);
  22. hex[2*i + 1] = to_hex_digit(bin[i] & 0xf);
  23. }
  24. hex[i * 2U] = 0U;
  25. }