Pages: [1]   Go Down
Author Topic: Parsing Float Values  (Read 218 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would like to know how I can parse the float value from a byte array or a method to parse a float value from the sd card.

I have a config file I want to use that contains a single float value on each line. The sd examples show reading the line contents into a byte array. How can I parse the float value from a byte array. Sample Data: 123.45

I searched the net and found some obscure examples none of which I could figure out or get working.  Any help would be appreciated.

Thanks:
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You will need to figure out if it is stored in a string format ("123.45"), or in a binary format; and in the case of a binary format, how they are stored.

In the case of a string format, you read one char at a time and multiple it by 10 for each digit read and then process the dot separately. Or to use a library function.

in the case of a binary format, you typically use a union or a pointer to convert it back.
Logged

Offline Offline
Edison Member
*
Karma: 18
Posts: 1170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If they are ascii characters, put them in a nul-terminated string and use the atof() function.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46063
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can use the strtok() function to throw away the part up to, and including the delimiter. Then, use strtok() again to get the token of interest, if the record contains other than just the number characters.
Logged

Pages: [1]   Go Up
Jump to: