Convert String to Int

Your source for all things Atmel.

Moderator: phalanx

alexgeek
Posts: 8
Joined: Wed Oct 14, 2009 12:31 pm

Convert String to Int

Post by alexgeek » Wed Oct 14, 2009 12:35 pm

I'm trying to figure out how to convert integers to strings so I can send them over rs232.

Atm the moment I'm using this:

Code: Select all

char b[5];
sprintf(b, "%d", i);
SendString(b,1);
But if I try to make this I get:
"avrdude: ERROR: address 0x0810 out of range at line 129 of Serial.hex"

And I have tried both:
LDFLAGS += -Wl,-u,vfprintf -lprintf_min
And
LDFLAGS += -Wl,-u,vfprintf -lprintf_flt

Is there some sort of include I need aswell or am I just doing it wrong?

Thanks
[url=http://www.alexgeek.co.uk]Tech Blog[/url]

stevech
Support Volunteer
Posts: 2907
Joined: Sat Jun 05, 2004 8:51 pm
Location: USA, California

Post by stevech » Wed Oct 14, 2009 7:12 pm

read about itoa() in the standard C libraries.
There's also an equivalent for longs.

you might want a bit larger char buffer for the largest possible int for whatever compiler you have, plus room for the terminating null byte on C strings.

for printf() you shouldn't need to customize the linker command - just choose a version of printf in the IDE settings.

I don't know what SendString() is - perhaps something custom within Arduino's libraries. I use plain C.

lehmanna
Posts: 74
Joined: Sat Jul 25, 2009 3:21 am

Post by lehmanna » Wed Oct 14, 2009 11:20 pm

With respect to restricted buffers like the one you're using, use snprintf(...) instead of sprintf(...).
Regards,
Alexander

krphop
Posts: 29
Joined: Sat Aug 29, 2009 6:31 am

Post by krphop » Thu Oct 15, 2009 12:11 am

Uhh, what? you want to convert an integer to a string to send it over the uart? A string will get converted to an int, then to binary before going out the urart, why not just send the int?

lehmanna
Posts: 74
Joined: Sat Jul 25, 2009 3:21 am

Post by lehmanna » Thu Oct 15, 2009 12:13 am

In addition to what krphop said, watch possible different endianness of the participating machines when transferring a raw int.
Regards,
Alexander

Polux rsv
Posts: 247
Joined: Wed Oct 11, 2006 11:34 am
Location: Gland, Switzerland

Post by Polux rsv » Thu Oct 15, 2009 3:49 am

Alexgeek want probably send strings to a simple terminal on the PC side. :roll:

Angelo

stevech
Support Volunteer
Posts: 2907
Joined: Sat Jun 05, 2004 8:51 pm
Location: USA, California

Post by stevech » Thu Oct 15, 2009 12:44 pm

lehmanna wrote:In addition to what krphop said, watch possible different endianness of the participating machines when transferring a raw int.
OP's goal was to send as text/ASCII.

alexgeek
Posts: 8
Joined: Wed Oct 14, 2009 12:31 pm

Post by alexgeek » Thu Oct 15, 2009 4:59 pm

I'm in the middle of trying to switch to USART atm so can't test anything at the moment.
What's the difference between sprintf and snprintf then?
Thanks very much.
[url=http://www.alexgeek.co.uk]Tech Blog[/url]

stevech
Support Volunteer
Posts: 2907
Joined: Sat Jun 05, 2004 8:51 pm
Location: USA, California

Post by stevech » Thu Oct 15, 2009 6:44 pm

alexgeek wrote:I'm in the middle of trying to switch to USART atm so can't test anything at the moment.
What's the difference between sprintf and snprintf then?
Thanks very much.
http://www.gnu.org/s/libc/manual/html_n ... tions.html

see
itoa()

schult
Posts: 17
Joined: Sat Jan 03, 2009 5:12 pm

Post by schult » Fri Oct 16, 2009 12:32 pm

stevech wrote:read about itoa() in the standard C libraries.
itoa() is not standard; however, there are plenty of implementations floating around. http://en.wikipedia.org/wiki/Itoa

stevech
Support Volunteer
Posts: 2907
Joined: Sat Jun 05, 2004 8:51 pm
Location: USA, California

Post by stevech » Fri Oct 16, 2009 5:12 pm

schult wrote:
stevech wrote:read about itoa() in the standard C libraries.
itoa() is not standard; however, there are plenty of implementations floating around. http://en.wikipedia.org/wiki/Itoa
Maybe - but every decent C compiler / IDE library I've worked with includes itoa() and friends.

schult
Posts: 17
Joined: Sat Jan 03, 2009 5:12 pm

Post by schult » Mon Oct 19, 2009 10:04 am

stevech wrote:
schult wrote:
stevech wrote:read about itoa() in the standard C libraries.
itoa() is not standard; however, there are plenty of implementations floating around. http://en.wikipedia.org/wiki/Itoa
Maybe - but every decent C compiler / IDE library I've worked with includes itoa() and friends.
You still won't find itoa() in documentation for the C standard library.

stevech
Support Volunteer
Posts: 2907
Joined: Sat Jun 05, 2004 8:51 pm
Location: USA, California

Post by stevech » Mon Oct 19, 2009 12:26 pm

schult wrote:
stevech wrote:
schult wrote: itoa() is not standard; however, there are plenty of implementations floating around. http://en.wikipedia.org/wiki/Itoa
Maybe - but every decent C compiler / IDE library I've worked with includes itoa() and friends.
You still won't find itoa() in documentation for the C standard library.
itoa() and many other functions in popular use are well documented by compiler providers.

schult
Posts: 17
Joined: Sat Jan 03, 2009 5:12 pm

Post by schult » Mon Oct 19, 2009 2:48 pm

stevech wrote:
schult wrote:
stevech wrote: Maybe - but every decent C compiler / IDE library I've worked with includes itoa() and friends.
You still won't find itoa() in documentation for the C standard library.
itoa() and many other functions in popular use are well documented by compiler providers.
Then you should have said to look in the documentation provided with the compiler's library, NOT in the C standard library. There are many resources that cover the standard library that do NOT cover itoa(), including numerous books and web references. Moreover, the behavior of non-standard functions may vary between different implementations, and if you don't look at the docs for the specific library you are using, you may introduce bugs. Never mind worrying about portability...

stevech
Support Volunteer
Posts: 2907
Joined: Sat Jun 05, 2004 8:51 pm
Location: USA, California

Post by stevech » Mon Oct 19, 2009 5:56 pm

schult wrote: Then you should have said to look in the documentation provided with the compiler's library, NOT in the C standard library.
My most humble apology. I am due 20 lashes.

Post Reply