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