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

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 Offline
Shannon Member
*****
Karma: 197
Posts: 12739
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Pick your poison...

strtol
strtoul
atol
Logged

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

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 Offline
Shannon Member
*****
Karma: 197
Posts: 12739
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


mystring.toInt()
Logged

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

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 Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Shannon Member
*****
Karma: 197
Posts: 12739
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I wonder why the Arduino folks used atol instead of strtol.

An example would be good.  smiley
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 197
Posts: 12739
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


The simplest possible usage of strtol...

Code:
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 Offline
Shannon Member
*****
Karma: 197
Posts: 12739
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Ensure the string parses to an integer...

Code:
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 Offline
Shannon Member
*****
Karma: 197
Posts: 12739
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Extract a list of integers from a string...

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

Pages: [1]   Go Up
Jump to: