Go Down

Topic: Converting a String to Long (Read 1 time) previous topic - next topic

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

Coding Badly


Pick your poison...

[font=Courier New]strtol
strtoul
atol
[/font]

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)'

Coding Badly


[font=Courier New]mystring.toInt()[/font]

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

PaulS

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.

Coding Badly

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.

zoomkat

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


An example would be good.  :)
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Coding Badly


The simplest possible usage of strtol...

Code: [Select]

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 );
}

Coding Badly


Ensure the string parses to an integer...

Code: [Select]

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 );
}

Coding Badly


Extract a list of integers from a string...

Code: [Select]

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 );
}

Go Up