tags:

views:

250

answers:

3

I need to convert a char array into int and float using C The array is like this

char* text = "15.34";

I also need to convert a float/int back into an array again

+7  A: 

Use atoi()/strtol() and atof()/strtod() library functions to convert from string.

To convert back use sprintf() with %d and %f format specifiers.

qrdl
A: 

Take a look at sscanf() and sprintf().

mouviciel
+5  A: 

You can use sscanf also. For example:

float fp = 0; sscanf( text, "%f", &fp );

To convert back use sprintf()

Naveen
One should check sscanf's return value to make sure it successfully converted the string into a float, too.
Chris Young