Bryan Martin wrote:
> I am attempting to move to C (winavr) from bascom for programming but
> getting hung up on some fundamentals.
>
> Given the following
>
> unsigned char byte_in = 0xA;
>
> I need to print the decimal value to UART so I want to see 10. Similar to
> printf("%d", byte_in). Lord I hope this makes sense.
static void
format(int num, char digits[6], char *width)
{
char ndx = 0;
if(num < 0) {
digits[ndx++] = '-';
num = -num;
}
else {
digits[ndx++] = ' ';
}
int d = 10000;
for(int i = 1; i < 6; i++) {
char n = num/d;
digits[ndx] = n + '0';
if(n) ndx++;
num %= d;
d /= 10;
}
*width = ndx;
}
int
main(int argc, char **argv)
{
char digits[6], width;
format(-672, digits, &width);
for(int i = 0; i < width; i++) {
putchar(digits[i]);
}
putchar('\n');
}Message
Re: [AVR-Chat] Simple casting winavr
2008-01-24 by Russell Shaw
Attachments
- No local attachments were found for this message.