Converting a Double Quoted String to an Integer

I have a String e.g. “95651” which is got the double quotes in it.

Getting rid of the quotes is easy enough using

String mySmallerString = myString.substring(1, myString.length()-1);

But I need to get the string into an Integer with the value of 95651. How do I do that?

I tried this

char * params = new char[mySmallerString.length() + 1];

strcpy(params, mySmallerString.c_str());

myInt = atoi(params);  

But myInt now contains 163 instead of 95651.

Is there a way I can first test of the String has Double quotes in it, remove them and the move the value into an Integer variable?

How about the easy way,

 myInt = mySmallerString.toInt()

BTW, your code works fine for me. How have you typed myInt? The number 163 is the value of the lower 8 bits of 95651, so it seems that myInt must be typed to an 8 bit type.

I don’t know why you chose to create params the way you did. Because you used new, I think you have to free the memory with delete [] params. The more usual way to do it would be,

char  params[mySmallerString.length() + 1];
strcpy(params, mySmallerString.c_str());
myInt = atoi(params);

But, that can be simplified, since there’s really no reason to copy the string,

myInt = atoi(mySmallerString.c_str());

And there is an even simpler way, with even less varuables involved

int myInt = myString.substring(1, myString.length()-1).toInt();

I didn’t check that I had declared myInt as an 8 bit unsigned Int!

Anyway, how do I check that myString has Double quotes in it both at the beginning and the end before I substring it?
Or is there a way I can extract the value between the Double Quotes using the Double Quotes as delimiters? Something like strtok

You could try

myString.replace("\"", "");
int myInt = myString.toInt();

But as always: Try using char* / char[] instead of String wherever possible :wink:

Now the issue I have is that my String is actually “95667.7” when I strip the double quotes out and move that to a float variable using toFloat() my float has a value of 95667.703125.

And 95667.9 becomes 95667.898438 as a float.

How do I get the float to be exactly the value of the String with a single decimal point.

That is not possible without rounding due to the way how floats are stored in memory (sums of powers of two).

But that is a common issue which everybody using float has to face.
You can use double which reduces the problem but can’t remove it completely either.

If you want to “print” a floating point variable with a set number of decimal places you need to do things like

  Serial.print(myFloat, 1);
  // or
  char txtNumber[16];
  snprintf(txtNumber, sizeof(txtNumber), "%.1f", myFloat);
1 Like

Can I use another data type if I just want a single decimal place on an always positive number? I need to be able to do some maths on the number, so it’s not just for printing purposes. I am adding 0.1 to number and need it to become a whole number every 10 times I add 0.1

You multiply by 10 and use an integer type.

1 Like

I will put in another plug for this IEEE754 numerical conversion page:

The numbers above (95667.703125 and 95667.898438) are indeed the nearest 32-bit floats to the values you input.

And @ScruffR 's idea of using an integer type with a value multiplied by 10 is a good trick to remember too!