Pages: [1]   Go Down
Author Topic: String to int  (Read 741 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25709
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

Germany
Offline Offline
Faraday Member
**
Karma: 56
Posts: 2982
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25709
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
working with string instead of char* is already some overkill,
Did you mean, "working with String instead of char* is already some overkill,"?
« Last Edit: May 02, 2012, 03:33:51 am by AWOL » Logged

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

Germany
Offline Offline
Faraday Member
**
Karma: 56
Posts: 2982
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-wink

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25709
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Germany
Offline Offline
Faraday Member
**
Karma: 56
Posts: 2982
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

... 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  smiley-wink
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 )
« Last Edit: May 02, 2012, 01:55:36 pm by michael_x » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25709
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
typedef uint8_t byte;
Logged

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: