Page 1 of 2

Convert String to Int

Posted: Wed Oct 14, 2009 12:35 pm
by alexgeek
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

Posted: Wed Oct 14, 2009 7:12 pm
by stevech
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.

Posted: Wed Oct 14, 2009 11:20 pm
by lehmanna
With respect to restricted buffers like the one you're using, use snprintf(...) instead of sprintf(...).

Posted: Thu Oct 15, 2009 12:11 am
by krphop
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?

Posted: Thu Oct 15, 2009 12:13 am
by lehmanna
In addition to what krphop said, watch possible different endianness of the participating machines when transferring a raw int.

Posted: Thu Oct 15, 2009 3:49 am
by Polux rsv
Alexgeek want probably send strings to a simple terminal on the PC side. :roll:

Angelo

Posted: Thu Oct 15, 2009 12:44 pm
by stevech
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.

Posted: Thu Oct 15, 2009 4:59 pm
by alexgeek
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.

Posted: Thu Oct 15, 2009 6:44 pm
by stevech
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()

Posted: Fri Oct 16, 2009 12:32 pm
by schult
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

Posted: Fri Oct 16, 2009 5:12 pm
by stevech
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.

Posted: Mon Oct 19, 2009 10:04 am
by schult
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.

Posted: Mon Oct 19, 2009 12:26 pm
by stevech
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.

Posted: Mon Oct 19, 2009 2:48 pm
by schult
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...

Posted: Mon Oct 19, 2009 5:56 pm
by stevech
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.