Offline
Newbie
Karma: 0
Posts: 42
|
 |
« on: May 02, 2012, 12:55:24 am » |
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);
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 15
|
 |
« Reply #1 on: May 02, 2012, 02:47:49 am » |
Maybe because int only goes up to 32767. Try using a long variable and the corresponding atol function.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19066
I don't think you connected the grounds, Dave.
|
 |
« Reply #2 on: May 02, 2012, 03:16:04 am » |
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.
|
|
|
|
Germany
Offline
Edison Member
Karma: 27
Posts: 1497
|
 |
« Reply #3 on: May 02, 2012, 03:28:45 am » |
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
Brattain Member
Karma: 138
Posts: 19066
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: May 02, 2012, 03:32:19 am » |
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.
|
|
|
|
Germany
Offline
Edison Member
Karma: 27
Posts: 1497
|
 |
« Reply #5 on: May 02, 2012, 04:25:54 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19066
I don't think you connected the grounds, Dave.
|
 |
« Reply #6 on: May 02, 2012, 04:46:17 am » |
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.
|
|
|
|
Germany
Offline
Edison Member
Karma: 27
Posts: 1497
|
 |
« Reply #7 on: May 02, 2012, 06:20:37 am » |
... 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 )
|
|
|
|
« Last Edit: May 02, 2012, 01:55:36 pm by michael_x »
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19066
I don't think you connected the grounds, Dave.
|
 |
« Reply #8 on: May 02, 2012, 06:30:06 am » |
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;
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #9 on: May 02, 2012, 07:35:30 am » |
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
Newbie
Karma: 0
Posts: 42
|
 |
« Reply #10 on: May 02, 2012, 04:15:01 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
|