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 Bryan Martin

Thanks Russell.  I will check this out also and see if it works for my goal 
of smaller code.


----- Original Message ----- 
From: "Russell Shaw" <rjshaw@netspace.net.au>
To: <AVR-Chat@yahoogroups.com>
Sent: Wednesday, January 23, 2008 8:58 PM
Subject: Re: [AVR-Chat] Simple casting winavr


> 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');
> }
>


--------------------------------------------------------------------------------


No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.516 / Virus Database: 269.19.9/1238 - Release Date: 1/22/2008 
8:12 PM

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.