String to int

Hi, I found this code snipet for converting a string to an int, it works great with numbers up to 5 digits but after that it fails.

The code below will work but if I change viewCount to "123456" then my int become a negative number.

Any ideas?

Thanks.

Phil

viewCount="12345";
char this_char[viewCount.length() + 1];
viewCount.toCharArray(this_char, sizeof(this_char)); 
int newCount = atoi(this_char); 
Serial.println(newCount);

Maybe because int only goes up to 32767. Try using a long variable and the corresponding atol function.

It's worth noting that the size (and therefore the range) of an "int" is platform-dependent; what works when compiled for a PC (which is what a lot of online examples will assume) may not work as expected when compiled for an Arduino.

On a PC, an "int" is typically four bytes long (32 bits) and represents the range -231 to 231-1, whereas on the Arduino, it is two bytes (16 its), and represents the range -215 to 215-1

On an arduino, working with string instead of char* is already some overkill, IMO

char* viewCount="1234567";
long number = atol(viewCount);

should already work fine

working with string instead of char* is already some overkill,

Did you mean, "working with String instead of char* is already some overkill,"?

AWOL:

working with string instead of char* is already some overkill,

Did you mean, "working with String instead of char* is already some overkill,"?

Thanks for giving me the chance to clarify:

lowercase "string" is just a confusing word for a char array.

A "String object, which gives you more functionality at the cost of more memory" is a thing I'd prefer to avoid on a small Arduino UNO, unless this additional functionality is really what you need and don't want to implement in plain c. That is more than charAt() and toCharArray() methods :wink:

lowercase "string" is just a confusing word for a char array.

Nothing confusing there; a string is a char array that is null-terminated, but a char array doesn't have to contain a string.

AWOL:
... a string is a char array that is null-terminated, but a char array doesn't have to contain a string.

You're right, point taken.

string sample="Some Sample Text"; // error: 'string' does not name a type  ;)
byte ba[] = {50, 100, 200, 0};    // usually omitting "unsigned" is laziness, 
                                  // but for non-character chars, one might choose this type if possible
                                  // ( signed byte is not allowed, though )

usually omitting "unsigned" is laziness,

In the case of "byte", not so - the type is defined to be unsigned, so adding an extra "unsigned" would be tautological.
If the array were of type "char", then yes, omitting an "unsigned" could be deemed lazy (but not by many).

typedef uint8_t byte;
string sample="Some Sample Text";

This string is a completely different beast, and only an asshole would have chosen such a generic term, that already had a quite well defined (but different) meaning, for a class name.

This worked great, thanks,

Phil

On an arduino, working with string instead of char* is already some overkill, IMO

char* viewCount="1234567";
long number = atol(viewCount);

should already work fine