avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Error converting int to string


From: Graham Davies
Subject: Re: [avr-gcc-list] Error converting int to string
Date: Fri, 3 Dec 2004 16:45:47 -0500

Leo,

Your problem could be that you've allocated only three bytes for lbuf and
when atoi() has a three digit number to convert it's going to produce a four
byte string, including the terminating zero.  If you're using the simulator
you should be able to see this happen and figure out what gets stomped on.

Graham.


----- Original Message ----- 
From: "Leo Hendrawan" <address@hidden>
To: <address@hidden>
Sent: Friday, December 03, 2004 11:22 PM
Subject: [avr-gcc-list] Error converting int to string


> Hello all,
>
>     i'm working on my function which parse the ip addr int type variable
into
>     strings. but i have some problem to do the reverese process (make an
array
>     string ended with '\0' from int varible type ip address).
>     i put my code below...
>     the desired ip address is: 167.205.67.1,
>     but when i use AVR studio 4 simulation (i use winavr20040404)
>     at the line:
>     itoa(temp,lbuf,10);              // the problem is right here
>     it always make the lower value of ipaddr2[0] to 0, which result the
wrong
>     ip address string: "167.0.67.1"
>     does anyone have any suggestion why this happens?
>
>     best regards,
>
>     Leo Hendrawan
>
>
>     //////////////////////////////////
>     // My code:
>     //////////////////////////////////
>     char volatile buffer[20];
>     char lbuf[3];
>
>
>     unsigned int volatile ipaddr1[2],ipaddr2[2];
>
>     void main(void)
>     {
>             ............
>
>             char dot[]=".";
>     int volatile temp;
>
>     ipaddr2[0] = (167<<8) | 205;
>     ipaddr2[1] = (67<<8) | 1;
>
>     temp = ipaddr2[0]>>8;
>     itoa(temp,lbuf,10);              // the problem is right here
>     strcpy((char*)buffer, lbuf);
>     strcat((char*)buffer,dot);
>     clear(lbuf);
>
>     temp = ipaddr2[0]&0xff;
>     itoa(temp,lbuf,10);
>     strcat((char*)buffer, lbuf);
>     strcat((char*)buffer,dot);
>     clear(lbuf);
>
>     temp = ipaddr2[1]>>8;
>     itoa(temp,lbuf,10);
>     strcat((char*)buffer, lbuf);
>     strcat((char*)buffer,dot);
>     clear(lbuf);
>
>     temp = ipaddr2[1]&0xff;
>     itoa(temp,lbuf,10);
>     strcat((char*)buffer, lbuf);
>     }
>
>
>
> _______________________________________________
> avr-gcc-list mailing list
> address@hidden
> http://www.avr1.org/mailman/listinfo/avr-gcc-list
>
>
>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]