Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

conversion from string to float

Status
Not open for further replies.

mamech

Full Member level 3
Joined
Nov 9, 2010
Messages
176
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Activity points
3,135
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

Administrator
Joined
Mar 12, 2010
Messages
9,821
Helped
2,350
Reputation
4,694
Reaction score
2,272
Trophy points
1,413
Location
Southwest, USA
Activity points
62,361
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:
  • Like
Reactions: mamech

    mamech

    Points: 2
    Helpful Answer Positive Rating

andre_luis

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,418
Helped
1,175
Reputation
2,369
Reaction score
1,169
Trophy points
1,403
Location
Brazil
Activity points
54,768
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

Administrator
Joined
Mar 12, 2010
Messages
9,821
Helped
2,350
Reputation
4,694
Reaction score
2,272
Trophy points
1,413
Location
Southwest, USA
Activity points
62,361
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:

**broken link removed**

Code:
    short ASCIIConvert( char value )
    {

      return (short) (value - 48);

    } // ASCIIConvert

You can "roll your own" routine to fit your needs.

---------- 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;
}

Customize it to your needs.

---------- 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
Joined
Nov 9, 2010
Messages
176
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Activity points
3,135
Thanks, I was searching about such function atoi().
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top