Problem Converting "12345" to 12345

Hello!
I am not sure how to convert a String to an int... I tried toInt() but it seems to give a value which I can't interpret. Speaking of Python, there is an int() function by which we can easily convert "1" to 1 or even "123456" to 123456. And that's exactly what I want to do using Arduino IDE...


Any help would be appreciated.
Thanks!

An int is 16 bits which means it can range from -32768 to +32767. Your number is larger than that.

If you made it an unsigned int then it would be 0..65536 which is still too small.

If you made it a long, then you have 32 bits and it will work but you can no longer use .toInt() but you can use .toFloat() and, if your variable is a long, it will be converted from a floating point number into a long integer

1 Like

You are trying to fit the number 123456 into an int. But it won't fit.

An int is for numbers -32768 to +32767. For bigger numbers, you need a long. A long is good for about negative 2 billion to positive 2 billion.

Or, you could go 'classic' and do the conversion yourself, processing each character until you encounter something other than 0-9. Sounds like work, though...

Even parsing each "character" and multiplying by the power representing its place, the precision is lost after x * 10^2.

char pluto[] = {"738"};
unsigned long result, power, number;

void setup() {
  Serial.begin(9600);

  Serial.println("n p n*10^p");
  for (int i = 0; i < strlen(pluto); i++) {
    power = (strlen(pluto) - i) - 1;
    number = pluto[i] - '0';
    Serial.print(number);
    Serial.print(' ');
    Serial.print(power);
    Serial.print(' ');
    Serial.print(number * pow(10, power), 0);
    Serial.print('\n');

    result = result + (number * pow(10, power));
  }
  Serial.print(result);
}

void loop() {
  // nobody puts pluto in a corner
}

... results in...

n p n*10^p
7 2 700
3 1 30
8 0 8
737

No doubt. But,

char pluto[] = {"123456"};
unsigned long result, power, number;

void setup() {
  Serial.begin(115200);

  Serial.println(pluto);
  number = 0;
  for (int i = 0; i < strlen(pluto); i++) {
    number = number * 10;
    number = number + pluto[i] - '0';
    Serial.println(number);
  }
}

void loop() {
}
20:38:11.972 -> 123456
20:38:11.972 -> 1
20:38:11.972 -> 12
20:38:11.972 -> 123
20:38:11.972 -> 1234
20:38:11.972 -> 12345
20:38:11.972 -> 123456

seems to work okay here. Must be the baud rate :roll_eyes:.

1 Like

.toInt() returns a long.
Yes, it might be a bit confusing.

Ah, much better. I was hoping pow() would be a few powers of 10.

I think it has to do with floating-point rounding error, and that pow() computes using an approximation (I'm guessing using exp() and log() internally). If you want integer math, you have to use integer math, at least in this case.

More likely, just the actual numeric accuracy of floats in this instance, not 'rounding error' per se. But I'm not interested enough to look under the covers, the OP should, if curious, do so.

Good to know.

Wow! So there is a way!
So instead of printing the output in Serial monitor, can I store it in an int variable?

From post #7
unsigned long result, power, number;
It is in an unsigned long int at the end of the code.
The following is worth reading"
The Evils of Arduino Strings | Majenko's Hardware Hacking Blog

No, no, no!!!
Six digits is too big to fit in an int! You need a long for that!

Oh yeah! I am literally so used to Python that I keep on forgetting that the long data type even exists... So sorry about that!

When I try to add a String variable instead of a String in line 1 of the code you've mentioned, I get an error:

error: cannot convert 'String' to 'char' in initialization
char pluto[] = {test};
^
exit status 1
cannot convert 'String' to 'char' in initialization

Hope there is a way to add a String variable instead of a direct String...
Or else, for me the whole point of this conversion won't help! Sorry for not mentioning that I needed to convert a String variable. Technically, both should mean the same thing though...

This is the slightly changed code:

String text="123456";
char pluto[] = {text};
unsigned long result, power, number;

void setup() {
  Serial.begin(115200);

  Serial.println(pluto);
  number = 0;
  for (int i = 0; i < strlen(pluto); i++) {
    number = number * 10;
    number = number + pluto[i] - '0';
    Serial.println(number);
  }
}

void loop() {
}

Try this:

String text="123456"; // the String you want to convert to a char array

char pluto[11]; // the char array you want to hold the string
// make sure that the char array is big enough
// I made it size 11 which is enough for a string of 10 characters

strcpy(pluto, text.c_str()); // this line does the conversion to char array
1 Like

Oh yes! It works perfectly.
Thanks to all for their efforts, appreciate the help! :grinning:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.