system
December 6, 2012, 6:10am
1
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
system
December 6, 2012, 6:22am
3
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)'
system
December 6, 2012, 11:46am
5
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
system
December 6, 2012, 5:28pm
6
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.
liveitnerd:
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.
I wonder why the Arduino folks used atol instead of strtol.
An example would be good.
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 );
}
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 );
}
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 );
}