SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By alexgeek
#83024
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
By stevech
#83036
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.
By lehmanna
#83047
With respect to restricted buffers like the one you're using, use snprintf(...) instead of sprintf(...).
By krphop
#83049
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?
By lehmanna
#83050
In addition to what krphop said, watch possible different endianness of the participating machines when transferring a raw int.
By Polux rsv
#83055
Alexgeek want probably send strings to a simple terminal on the PC side. :roll:

Angelo
By stevech
#83070
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.
By alexgeek
#83082
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.
By stevech
#83086
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()
By schult
#83127
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
By stevech
#83137
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.
By schult
#83256
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.
By stevech
#83269
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.
By schult
#83284
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...
By stevech
#83299
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.