Go Down

Topic: Convert long to hex string (Read 1 time) previous topic - next topic

simplesi

Week 1 Arduino newbie:)
I'm decoding IR codes using Ken Sherriff's IRLibrary but I'm having problems in checking for particular codes

This code
Code: [Select]
...
unsigned long codeValue;
...
(use lib to get a value)
...
   Serial.println(codeValue);
    String sCodeValue = String(2576,HEX);
    Serial.println(sCodeValue);
    sCodeValue = String(codeValue,HEX);
    Serial.println(sCodeValue);


produces

2576
a10
2576

whereas I was expecting/hoping for

2576
a10
a10

Can someone tell me what language error I've made please? :)
(Basically I'd like to compare sCodeValue against a set of values but I have those values in Hex so I don't want to have to translate them all to decimal)

regards

Simon



robtillaart


This is a known bug in the String class, which is reported. - http://code.google.com/p/arduino/issues/detail?id=577&q=String%20class -

The fix is in the file: Wstring.cpp   (windows under C:\Program Files (x86)\arduino-0022\hardware\arduino\cores\arduino )

String::String( const unsigned long value, const int base )
{
  char buf[33];   
  ultoa(value, buf, 10);
  getBuffer( _length = strlen(buf) );
  if ( _buffer != NULL )
    strcpy( _buffer, buf );
}

should be changed in

String::String( const unsigned long value, const int base )
{
  char buf[33];   
  ultoa(value, buf, base);
  getBuffer( _length = strlen(buf) );
  if ( _buffer != NULL )
    strcpy( _buffer, buf );
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

simplesi

Phew - I'm glad it wasn't me :)
Thanks

Si

simplesi

Applied your fix - working fine now - Ta :)
Simon

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy