Help converting a float to a string... (solved)

Well another day, another wrestling match with cstrings...

I'm having trouble converting a float to a cstring.

When I run the following:

  float height = 1.75;
  char response[20];
  dtostrf(response, 5, 2, height); 
  Serial.println(response);

I get the error:

"cannot convert 'char*' to 'double' for argument '1' to 'char* dtostrf(double, signed char, unsigned char, char*)'"

I don't imagine anyone knows what I'm doing wrong?

  dtostrf(height, 5, 2, response);

See dtostrf()

Thank you!

When all else fails, RTFM :slight_smile:

1. This is the syntax of dtostrf() function.


Figure-1:

2. This is your declaration.

dtostrf(response, 5, 2, height);

3. Try to find the errors in your declaration in view of Step-1.

4. This is my declaration/codes that give correct result.

void setup()
{
  Serial.begin(9600);
  double height = 1.75;
  char response[20];
  dtostrf(height, 4, 2, response);//, 5, 2, height);
  Serial.println(response);
  for(int i = 0; i<4; i++)
  {
    Serial.print(response[i], HEX);  //shows the ASCII codes of 1 . 7 5 : 31 2E 37 35
  }
}

void loop()
{
  
}

4. This is the screenshot containing my result of the sketch of Step-3
sm25.png

5. This is the ASCII Table to check the correctness of the codes shown in Fig-1.


Figure-2:

sm25.png

UKHeliBob:
When all else fails, RTFM :slight_smile:

Read the sweet examples?

GolamMostafa:
Read the sweet examples?

Something like that