# conversion from string to float

Status
Not open for further replies.

#### mamech

##### Full Member level 3
Hello Everyone

I want to convert some value from string to float or int. The libraries in MikroC permits only conversion from float or int to string.

How can I do this?

Thanks

#### bigdogguru

Take a look in the standard library header, stdlib.h:

stdlib.h

atoi()

atof()

Wiki probably not the best reference for the C standard library, but I haven't had my coffee yet this morning and you get the idea.

However, atoi() is not clearly defined or part of the actual "Standard," but is usually included in the standard library of C.

Check MikroC's library documenation for details.

Also, there are also plenty of short routines available, in the embedded C world, if you're trying to conserve precious program memory and other limited resources to accomplish the task with limited resources.

If in the future you happen to by making the conversion the other direction. Be advised, I would avoid the use of their FloatToStr() and IntToStr() functions, the both require large arrays of char and seem to create more problems than they solve.

A form of sprintf(), which is defined by the "Standard" and usually included in the stdio.h, can achieve a more effective result. Sprintf() can be a memory hog and is usually accompanied by several limited and smaller footprint versions in embedded C compilers.

sprintf()

Last edited:
mamech

### mamech

Points: 2

#### andre_luis

##### Super Moderator
Staff member
mamech

I take a look on web and found some occurences.
Canno´t sugest anyone in special, due my proxy blocks everything.

Search for criteria : str2float.c.

+++

#### bigdogguru

MikroC's limited implementations of sprintf():

Sprint Library (mC PRO)

sprintl()

sprinti()

---------- Post added at 16:47 ---------- Previous post was at 16:04 ----------

Back to your original question, conversion of ascii to integer, here is the basis for most algorithms:

Code:
    short ASCIIConvert( char value )
{

return (short) (value - 48);

} // ASCIIConvert

---------- Post added at 17:30 ---------- Previous post was at 16:47 ----------

Found this code snippet laying around:

Code:
int atoi(char s[])
{
int i,n;

n=0;

for(i=0; s[i]>='0' && s[i]<='9'; i++)
n=10*n + (s[i] - '0');

return n;
}

---------- Post added at 17:46 ---------- Previous post was at 17:30 ----------

I checked the MikroC User Manual, it does include a limited version of stdlib.h with an atoi() function:

MikroC Pro User Manual

int atoi(char *s);

Function converts the input string s into an integer value and returns the value.
The input string s should consist exclusively of decimal digits, with an optional
whitespace and a sign at the beginning. The string will be processed one character
at a time, until the function reaches a character which it doesn’t recognize
(including a null character).

#### mamech

##### Full Member level 3
Thanks, I was searching about such function atoi().

Status
Not open for further replies.