dec.c 532 B

123456789101112131415161718192021222324252627282930313233
  1. /*
  2. * Copyright (c) 2019 Oticon A/S
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <sys/util.h>
  7. uint8_t u8_to_dec(char *buf, uint8_t buflen, uint8_t value)
  8. {
  9. uint8_t divisor = 100;
  10. uint8_t num_digits = 0;
  11. uint8_t digit;
  12. while (buflen > 0 && divisor > 0) {
  13. digit = value / divisor;
  14. if (digit != 0 || divisor == 1 || num_digits != 0) {
  15. *buf = (char)digit + '0';
  16. buf++;
  17. buflen--;
  18. num_digits++;
  19. }
  20. value -= digit * divisor;
  21. divisor /= 10;
  22. }
  23. if (buflen) {
  24. *buf = '\0';
  25. }
  26. return num_digits;
  27. }