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