Pages: [1]   Go Down
Author Topic: Convert long to hex string  (Read 1289 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
...
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? smiley
(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


Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13478
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Rob Tillaart

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Phew - I'm glad it wasn't me smiley
Thanks

Si
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Applied your fix - working fine now - Ta smiley
Simon
Logged

Pages: [1]   Go Up
Jump to: