Offline
Newbie
Karma: 0
Posts: 4
|
 |
« on: December 06, 2012, 01:10:58 am » |
Hi,
I'm writing a web interface to control some WS2801 LED's and am wanting to convert string to long. The code I have takes a string in the format "0xFFDD00" and needs to be put in to the following line:- ledStripStatus[0] = 0xFFDD00;
How can I get this converted to be done?
Thanks, LiveItNerd
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 118
Posts: 10167
|
 |
« Reply #1 on: December 06, 2012, 01:12:35 am » |
Pick your poison...
strtol strtoul atol
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 4
|
 |
« Reply #2 on: December 06, 2012, 01:22:03 am » |
Sweet. Thanks! I tried all of them but I get this instead.
error: cannot convert 'String' to 'const char*' for argument '1' to 'long unsigned int strtoul(const char*, char**, int)'
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 118
Posts: 10167
|
 |
« Reply #3 on: December 06, 2012, 01:46:17 am » |
mystring.toInt()
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 4
|
 |
« Reply #4 on: December 06, 2012, 06:46:15 am » |
When I try to use toInt() it returns 0.
colourString = "0xFF0000"; ledStripStatus[0]=colourString.toInt();
... where colourString is a String and ledStripStatus Long Array. Sorry for not understanding this too well. I'm quite new to Processing and I'm still learning the syntax for it all.
Thanks, LiveItNerd
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 314
Posts: 35518
Seattle, WA USA
|
 |
« Reply #5 on: December 06, 2012, 12:28:38 pm » |
You need to quit using Strings. While you are figuring out to do that, the toCharArray() method will extract the contents of the String as a char array that can be passed to strtol(), etc.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 118
Posts: 10167
|
 |
« Reply #6 on: December 06, 2012, 01:29:27 pm » |
When I try to use toInt() it returns 0. Huh. So it would. I wonder why the Arduino folks used atol instead of strtol. In any case, PaulS' suggestion will work.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 50
Posts: 6549
Arduino rocks
|
 |
« Reply #7 on: December 06, 2012, 01:35:05 pm » |
I wonder why the Arduino folks used atol instead of strtol. An example would be good. 
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 118
Posts: 10167
|
 |
« Reply #8 on: December 07, 2012, 04:36:00 am » |
The simplest possible usage of strtol... void setup( void ) { Serial.begin( 115200 ); }
void loop( void ) { char buffer[20]; long value; strncpy( buffer, "0xFFDD00", sizeof(buffer) ); buffer[sizeof(buffer)-1] = 0; value = strtol( buffer, NULL, 0 ); Serial.println( value ); delay( 1000 ); }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 118
Posts: 10167
|
 |
« Reply #9 on: December 07, 2012, 04:38:14 am » |
Ensure the string parses to an integer... void setup( void ) { Serial.begin( 115200 ); }
void loop( void ) { char buffer[20]; char* endptr; long value; strncpy( buffer, "badnews", sizeof(buffer) ); buffer[sizeof(buffer)-1] = 0; value = strtol( buffer, &endptr, 0 ); if ( endptr != &buffer[0] ) { Serial.println( value ); } else { Serial.println( F( "Bad news." ) ); } delay( 1000 ); }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 118
Posts: 10167
|
 |
« Reply #10 on: December 07, 2012, 04:42:54 am » |
Extract a list of integers from a string... void setup( void ) { Serial.begin( 115200 ); Serial.println( F( "----------" ) ); }
void loop( void ) { char buffer[20]; char* headptr; char* endptr; long value; strncpy( buffer, "1 2 3 4 5", sizeof(buffer) ); buffer[sizeof(buffer)-1] = 0; headptr = &buffer[0]; value = strtol( headptr, &endptr, 0 ); while ( endptr != headptr ) { Serial.println( value ); headptr = endptr; value = strtol( headptr, &endptr, 0 ); } Serial.println( F( "----------" ) );
delay( 1000 ); }
|
|
|
|
|
Logged
|
|
|
|
|
|