This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

atof : Converting string to float

Other Parts Discussed in Thread: TM4C123GH6PGE

Hello All

   PART=TM4C123GH6PGE

   Is there a way to convert string to float? I tried the following:

...
#include <stdlib.h>
#include "utils/ustdlib.h" ... int main{ ... char buf[30]="3.2";
value = atof(buf); UARTprintf("%s\n%f\n",buf,value);

   value = strtof(buf);
   UARTprintf("%s\n%f\n",buf,value);
... }

but I get an error while compiling:

/usr/bin/../lib/gcc/arm-none-eabi/4.8.3/../../../../arm-none-eabi/lib/armv7e-m/softfp/libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk'
make: *** [gcc/main.axf] Error 1

Thanks in advance.

  • It appears the gcc library is using memory allocation in the conversion.

    If you can restrict your input sufficiently you can write a simple version yourself. A complete implementation can be complex since you have to be sure you cover all of the format variations.

    Also check for strtod

    Robert

  • Hi Mike,

         At my copy of ustdlib.h, I don't have strtof(). What I have is ustrtof(). See, below function prototype. Check again at your ustdlib.c file.

         extern float ustrtof(const char * restrict nptr, const char ** restrict endptr);

    -kel

         

  • I did try ustrtof.

    Here is an example of my code(copied from cplusplus.com):

    char szOrbits[] = "686.97 365.24";
    char* pEnd;
    float f1, f2;
    f1 = ustrtof (szOrbits, &pEnd);
    f2 = ustrtof (pEnd, NULL);
    UARTprintf("One martian year takes %f Earth years.\n", f1/f2);
    

    After successful compilation, I get:

    One martian year takes ERROR Earth years.

     

  • I don't believe UARTprintf() supports the format "%f". Maybe try looking at f1 and f1 variables in the debugger.

  • Hi Mike,

         Norman is correct. UARTprintf only supports the following format below. In the past I used ftoa to print float values to PC.

    //! - \%c to print a character
    //! - \%d or \%i to print a decimal value
    //! - \%s to print a string
    //! - \%u to print an unsigned decimal value
    //! - \%x to print a hexadecimal value using lower case letters
    //! - \%X to print a hexadecimal value using lower case letters (not upper case
    //! letters as would typically be used)
    //! - \%p to print a pointer as a hexadecimal value
    //! - \%\% to print out a \% character

    -kel

  • Hi Mike,

    There are a couple of problems.

    Firstly, UARTprintf does not handle floating point numbers.

    So, instead of:

        UARTprintf("The float = %f", theFloat);

    You can write:

        sprintf(theString, "%f", theFloat);

        UARTprintf("The float = %s", theString);

    Secondly, I tried atof in CCS and it does not work! for instnace, 123.456 comes out as 446676599.000000 (when I use double) or 446676608.0000 (when I use float).

    The following routine, from K&R page 71 works fine:

    #include <ctype.h>


    /* atof: convert string s to double */
    double atof(char s[])
    {
        double val, power;
        int i, sign;


        for (i = 0; isspace(s[i]); i++) /* skip white space */
            ;
        sign = (s[i] == '-') ? -1 : 1;
        if (s[i] == '+' || s[i] == '-')
            i++;
        for (val = 0.0; isdigit(s[i]); i++)
            val = 10.0 * val + (s[i] - '0');
        if (s[i] == '.')
            i++;
        for (power = 1.0; isdigit(s[i]); i++) {
            val = 10.0 * val + (s[i] - '0');
            power *= 10;
        }
        return sign * val / power;
    }

    I've mixed float and double values (which word) - just select your choice.

    Regards, Vito

  • Hi Mike!

    did you solved the problem of atof()?

    same problem I am facing in Tiva TM4C123G controller.

    If you had any solution which will help me? pls. provide the same.

    Thanks and regards,

    Pramod