You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
795 B

/** @file uint8toa.c
@author M. P. Hayes, UCECE
@date 15 May 2007
@brief
*/
#include "system.h"
/** Convert 8 bit unsigned number to decimal ASCII string. This uses
much less memory than sprintf.
@param num number to convert
@param str pointer to array of at least 4 chars to hold string
@param leading_zeroes non-zero to pad with leading zeroes. */
void
uint8toa (uint8_t num, char *str, bool leading_zeroes)
{
uint8_t d;
uint8_t i;
uint8_t const powers[] = {100, 10, 1, 0};
for (i = 0; (d = powers[i]); i++)
{
uint8_t q;
q = num / d;
if (leading_zeroes || q || d == 1)
{
*str++ = '0' + q;
num -= q * d;
leading_zeroes = 1;
}
}
*str = '\0';
}