Convert String to Int

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:

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

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.

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

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?

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

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

Angelo

lehmanna:
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.

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.

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.

http://www.gnu.org/s/libc/manual/html_n … tions.html

see

itoa()

stevech:
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

schult:

stevech:
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.

stevech:

schult:

stevech:
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.

schult:

stevech:

schult:
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.

stevech:

schult:

stevech:
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…

schult:
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.

stevech:

schult:
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.

*zap* *zap* ... :lol:

Did you even read the whole thread? :?:

“atoi” is what you’re looking for in terms of a library function, or maybe even so for a manual implementation of atoi. A link for the latter has been posted during this thread.