String to float

Hi,

I try to convert String (not char array) to float. I thought there most be at least a post about it but I didn't find it.

My try (works as String to int version with atoi instead of atof):

int convertStringtoFloat(String convert){
	String toconvert=convert;
	toconvert = toconvert + 0;
	char test_as_char[toconvert.length()];
	toconvert.toCharArray(test_as_char, toconvert.length());
	float F = atof(test_as_char);

	return F;
}

But a String with 115.22 ends up as float 115.00

How do I do the convertion?

Thanks
Robert

I don't know the answer to your question but someone will be along soon to tell you not to use Strings because there are problems with the library.

OK. Do you know what the problems are?
I use them to receive serial messages of all kind. Very handy and no problems so far.

This may be of interest http://arduino.cc/forum/index.php/topic,124367.0.html

But a String with 115.22 ends up as float 115.00

The String "115.22" has 6 characters. You allocate space to hold 6 characters Then, you tell the String object to fill the array that can hold 6 characters. Since the String class knows, but you apparently don't, that strings need to be NULL terminated, only "115.2" is extracted.

Why are you duplicating the String? Why are you adding 0 to a String? What the hell does that accomplish? I mean, think about it. A String is an object. How can you add an integer to an object? What is elephant + 7?

Why are you adding 0 to a String?

I got the StringtoInt function from (I think) the Arduino forum some time ago. It worked fine until I decided to not parse \n (which i use to signal end of message) into the String.
I don't get why the function stopped working. I thought the sting might no longer be terminated propperly.

Adding 0 was my try to to add the null termination
Dez Hex Okt ASCII
0 0x00 000 NUL

Obviously not correct but .. the function worked again.

From what you stated:

Then, you tell the String object to fill the array that can hold 6 characters. Since the String class knows, but you apparently don't, that strings need to be NULL terminated, only "115.2" is extracted.

most likely because the array got big enough.

Why are you duplicating the String?

You mean this?: String toconvert=convert;
Because I didn't change the function I got.

What the hell does that accomplish? I mean, think about it. A String is an object. How can you add an integer to an object? What is elephant + 7?

How do I correctly add the null termination? + NULL?

only "115.2" is extracted

So why do I then get 115.00 instead of 115.2 when using the function?

Thanks
Robert

How do I correctly add the null termination?

To what? The String is already NULL terminated. The toCharArray() method handles the NULL termination of the string.

So why do I then get 115.00 instead of 115.2 when using the function?

You get 115.00 because the float is printed to two decimal places. As to why the decimal part is lost, you have many places in the code that need fixing. Fix them, and then print stuff as you go, to see where the String, string, or float become incorrect.

I'm going to go out on a limb here and guess that, like compilation issues, something that is incorrect now, when fixed, will correct the issue that is occurring later.

Thanks a lot for the lesson. The main issue was that I didn't change the type of the return value when changing the function from StringtoInt into StringtoFloat.
So instead of "float convertStringtoFloat(String convert)" I used "int convertStringtoFloat(String convert)". Did you see this?
With all the print for debugging I found it.

In any case cleaning up the functions by removing the completely unnecessary +0 and the added overhead by doubling the String made things cleaner and less error prone.

Sometimes being only result orientated kicks back. I’ll dig more into functions I get from somewhere else next time. :blush:

The functions are now:

int convertStringtoInt(String convert){
	char test_as_char[convert.length()];
	convert.toCharArray(test_as_char, convert.length());
	int F = atoi(test_as_char);
	return F;
}

float convertStringtoFloat(String convert){
	char test_as_char[convert.length()];
	convert.toCharArray(test_as_char, convert.length());
	float F = atof(test_as_char);
	return F;
}

Regards,
Robert

char test_as_char[convert.length()];

Does this account for the string null terminator?

The String class already has a toInt() method.

Your arrays are still to small. You are not allowing room for the NULL terminator. The toCharArray() method will put the NULL in the array, but YOU need to allow room for it.

And, yes you have to have the correct return type, and I missed that.

PS. I don't like F as the name of an int.

Hi,

I indeed just noticed that my integers were one digit too short.
So I changed it:

int convertStringtoInt(String convert){
	char test_as_char[convert.length()+1];
	convert.toCharArray(test_as_char, convert.length()+1);
	int myInt = atoi(test_as_char);
	return myInt;
}

float convertStringtoFloat(String convert){
	char test_as_char[convert.length()+1];
	convert.toCharArray(test_as_char, convert.length()+1);
	float myFloat = atof(test_as_char);
	return myFloat;
}

It didn't cause problems with the toFloat though. That's why I didn't notice it rightaway.

The toInt() does indeed do the job well too.

Thanks again.
Robert

It didn't cause problems with the toFloat though

yet

AWOL:

It didn't cause problems with the toFloat though

yet

:slight_smile: most likely. I changed it.
I just don't understand why it didn't instantly shortened the float.

Anyway, question solved and things learned.

robvoi:
I just don't understand why it didn't instantly shortened the float.

Because when declaring:

char someArray[5];

Doesn't mean someArray[5] doesn't exist, it means that you could possibly be changing the value of some other variable down the road.

Ah, thanks for the explanation.