convert char array to float

For some reason this does not work in my complete code , only when i use it seperately


float actual char outsideTemp[10]

actual = atof(outsideTemp);

Does somebody know a easy alternative ?

the char array is read from a site, by numberOfChar = buienradarClient.readBytesUntil('"',outsideTemp,6);

in this case i what to used outsideTemp, to calculate with

Does somebody know a easy alternative ?

There is nothing easier than atof(). If it “doesn’t work in your code”, your code is wrong.

Post the real code. In the single statement, outsideTemp is an array, but it is NOT NULL terminated. So, it is NOT suitable for passing to atof(). You KNOW where the NULL goes (the readBytesUntil() method tells you that), so put the NULL in the proper place (assuming that there is room for it; of not, make room).

I think Paul has probably nailed the problem; no NULL terminator. Given just the fragment, you need to do something like:

// code, probably in loop...
   numberOfChar = buienradarClient.readBytesUntil('"',outsideTemp,6);

   outsideTemp[numberOfChar] = '\0';                         // You need this...

   actual = atof(outsideTemp);  
// rest of code...

thanks for the fast reply

mmh i thought the readBytesUntil sets his own NULL at the end of its stream

i will put it in my code

but still it is not working for some reason

mmh i thought the readBytesUntil sets his own NULL at the end of its stream

i will put it in my code

but still it is not working for some reason

It does not. If you made code changes, you need to post your code (ALL of it; in code tags) and explain what the code actually does, and how that differs from what you want. Provide whatever proof you can, too, that the code is not working.

'atof()' does not require a terminating null character. It ends at the first non-valid character. From the cplusplus.com page on 'atof()':- "The rest of the string after the last valid character is ignored and has no effect on the behavior of this function."

For instance, this works fine:-

void setup()
{
    Serial.begin(115200);
    char numString[] = "123.456abc";
    float dVal = atof(numString);
    Serial.println(dVal, 3);           // Prints "123.456"
}

void loop(){}

Edit: To be clearer, this works too:-

void setup()
{
    Serial.begin(115200);
    char numString[] = {'1','2','3','.','4','5','6'};
    float dVal = atof(numString);
    Serial.println(dVal, 3);           // Prints "123.456"
}

void loop(){}

atof()

1 Like

'atof()' does not require a terminating null character. It ends at the first non-valid character.

If I declare an array, with 4 values, and I put the characters '1', '.'. '1', and '2' in the array, and the next memory location(s) after the array contain characters that are digits (such as '9', '8', '7', 'Q'), atof() will read the '1', '.', '1', and '2' from my array and the '9', '8', '7', and 'Q' that follow my array, and return a value of 1.12987, which is NOT what I want.

YMMV.

The atof() function DOES require a NULL terminated string. That is NOT the same as saying that all the characters up to the NULL will be read.

PaulS: If I declare an array, with 4 values, and I put the characters '1', '.'. '1', and '2' in the array, and the next memory location(s) after the array contain characters that are digits (such as '9', '8', '7', 'Q'), atof() will read the '1', '.', '1', and '2' from my array and the '9', '8', '7', and 'Q' that follow my array, and return a value of 1.12987, which is NOT what I want.

YMMV.

The atof() function DOES require a NULL terminated string. That is NOT the same as saying that all the characters up to the NULL will be read.

See the edit to my last post. No null character is needed, as I said. (As long as there are no more valid characters following. Only then is a terminating null necessary.)

Edit: I should have added - 'readBytesUntil()' copied characters until the ". Replacing it with a terminating null character will make no difference to the outcome.

Edit2: Now I get it. I was expecting 'atof()' to magically stop at the end of the declared array.