float to char*

Everything about programming the platform and using the Waspmote API
Post Reply
Fernus88
Posts: 40
Joined: Mon Feb 17, 2014 3:08 pm

float to char*

Post by Fernus88 » Tue Nov 11, 2014 9:40 pm

hi


i wanna convert a float to char* because i want to use WIFI.send().

so i use the following command to make the conversion

float number=23.67;
sprintf(trying1, "%f", number);
USB.print(trying1);

and this is waht i get:

?

how can i do it correctly?

thank you

libelium-dev
Posts: 27967
Joined: Mon Sep 28, 2009 1:06 pm

Re: float to char*

Post by libelium-dev » Wed Nov 12, 2014 1:45 pm

Fernus88,

there is a function on the Utils class that does just this. It is called Utils.float2String().

See this example code:
http://www.libelium.com/development/was ... ert-types/

b_duf
Posts: 13
Joined: Thu Aug 07, 2014 1:59 pm

Re: float to char*

Post by b_duf » Mon Jan 05, 2015 8:32 pm

Hello,

Is there also a string2float function or something like that?
I store Waspmote frames on the sd card, after some time I want to get these frames from the sd card again to create the frame again and send it with zigbee. But how do I get the sensor values out of the frame from the sd card again?
I can get the bytes out of the memory card, but it doesn't work to convert them into a float value so i can create the frame again. I read the frame from the sd card into an unsigned char buffer, then i take the 4 bytes of the sensor values and tried to convert them into float values again, but it gave wrong values. I also tried swapping the bytes, because the frame swaps the bytes, but I didn't find a way yet that works to do this, i also get the wrong float values.
How can I solve this?

libelium-dev
Posts: 27967
Joined: Mon Sep 28, 2009 1:06 pm

Re: float to char*

Post by libelium-dev » Wed Jan 07, 2015 10:09 am

Hi b_duf,

The Utils library does not include a conversion functions from String to float. Maybe the AVR library includes some conversion functions.

Regards.

b_duf
Posts: 13
Joined: Thu Aug 07, 2014 1:59 pm

Re: float to char*

Post by b_duf » Wed Jan 07, 2015 8:27 pm

And is the AVR library included in the waspmote IDE?

libelium-dev
Posts: 27967
Joined: Mon Sep 28, 2009 1:06 pm

Re: float to char*

Post by libelium-dev » Fri Jan 09, 2015 10:17 am

Hi b_duf,

Yes, this library is included. Maybe this link can help you with your application:

http://www.arduino-hacks.com/float-to-s ... y-arduino/

Regards.

wwctech
Posts: 27
Joined: Mon Mar 27, 2017 8:46 pm

Re: float to char*

Post by wwctech » Fri Nov 03, 2017 4:36 pm

When I try to use the float2string utility, I receive the following error.
error: no matching function for call to 'WaspUtils::float2String(char [20])'
Is there another way I can go about this?
Thanks

libelium-dev
Posts: 27967
Joined: Mon Sep 28, 2009 1:06 pm

Re: float to char*

Post by libelium-dev » Mon Nov 06, 2017 10:06 am

Hi,

What code and API are you using?

You can also use dtostrf() function to convert from float to string

Code: Select all

  float float_val = 123.456789;
  char float_str[10];
  
  // use dtostrf() to convert from float to string: 
  // '1' refers to minimum width
  // '3' refers to number of decimals
  dtostrf( float_val, 1, 3, float_str);
Regards

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest