Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: [AVR-Chat] Simple casting winavr

2008-01-24 by Russell Shaw

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[7])
{
     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 -= n*d;
         d /= 10;
     }
     digits[ndx] = 0;
}

int
main(int argc, char **argv)
{
     char digits[7];

     format(-672, digits);

     puts(digits);
     putchar('\n');
}

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.