char* to int and vice versa

Hi,

A quick question, what functions should I use to convert between a char * and int in RIOT?

/Adeel

Well, in c any Pointer is an integer. If you want the plain numeric value, you can just cast the pointer to int. There is no conversion function

The char * is a string here. Basically what I want is the equivalent of sprintf().

/Adeel

Ah okay. You are looking for atoi() :wink:

atoi() converts from a string to an int. I want to convert an int to a string.

/Adeel

You can do that with snprintf an its Format specifires

Try

sprintf(a_string, "%d",the_int);

:slight_smile: Marc

Hi, sprintf (and it’s siblings) should be avoided in production code though, since in newlib these functions are quite large. I’m not sure if this exist on all platforms, but there is also atoi()'s counterpart itoa().

Cheers, Martine

Hi,

(( Meanwhile the original question was clarified. ))

Still I wanted to say that, in C99 there are specific types to convert a pointer to an integer type. Don't just cast a pointer to an int or a long. It is not portable.