Casting String to Double


#1

I am trying to cast a String to a Double. My code is:

int chgTThre(String s)
{
 
   if (isValidNumber(s)  == true) 
   {
        //float ham = s.toFloat();
        return 1;
   }   
   else
   {
       return 0;
   }
}

The following statement (float ham = s.toFloat) prevents the code from compiling but does not give a compile error (cloud just says flash is unsuccessful).

I also tried the code below but end up still getting “Flash unsuccessful.”

int setDouble(String s) {
    Double = atof(s);
}

I would appreciate help in the correct way to cast/convert a String to a Double?


#2

That code looks correct.

The only thing I can think of is that you have crossed the flash/RAM threshold. The ARM Cortex M0 on bluz doesn’t natively support floating point, so when you use float data types it has to import additional libraries at compile time. This can take up extra flash and RAM.

If you were crossing the threshold of allowable flash or RAM, however, you should see a different error in the IDE. But, you can check. Once you conpile, there is a little info window in the IDE that shows the size of the binary. Can you paste that info here?


#3

Thanks @eric. I noticed the following in the raw output

/usr/local/gcc-arm-embedded/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/bin/ld: target/workspace.elf section `.text' will not fit in region `APP_FLASH'
/usr/local/gcc-arm-embedded/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/bin/ld: region `APP_FLASH' overflowed by 1888 bytes
collect2: error: ld returned 1 exit status
../../../build/module.mk:216: recipe for target 'target/workspace.elf' failed
make[1]: Leaving directory '/firmware/modules/bluz/user-part'
make[1]: *** [target/workspace.elf] Error 1
../build/recurse.mk:11: recipe for target 'modules/bluz/user-part' failed

===============================================
I am happier if I can convert the String to Double. The following code compiles but gives wrong answer for d:

        double d;
        sscanf(s, "%d", &d);
        thre_truck=d;

#4

Yes, it seems your app code is overflowing the available flash space.

Which firmware version are you using? There is more flash available to the user app in 2.X versions, so you can upgrade if you are on 1.X.

Otherwise, you will most likely need to find ways to trim your program if you need it to fit. Eliminating unneeded libraries and variables is the best place to start.


#5

Thanks @eric.

I am using firmware 2.3.5 but will change to int to save space.