Go Down

Topic: String to int (Read 858 times) previous topic - next topic

philspitler

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


Code: [Select]


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

FRiC

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

AWOL

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
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

michael_x

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

AWOL

#4
May 02, 2012, 10:32 am Last Edit: May 02, 2012, 10:33 am by AWOL Reason: 1
Quote
working with string instead of char* is already some overkill,

Did you mean, "working with String instead of char* is already some overkill,"?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

michael_x


Quote
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  ;)


AWOL

Quote
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

michael_x

#7
May 02, 2012, 01:20 pm Last Edit: May 02, 2012, 08:55 pm by michael_x Reason: 1

... 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 )

AWOL

Quote
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).

Code: [Select]
typedef uint8_t byte;
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Code: [Select]
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.

philspitler

This worked great, thanks,

Phil



Quote
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

Go Up