Pages: [1]   Go Down
Author Topic: String constructor bug  (Read 1449 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The following code outputs "13" rather than "D"

Code:
unsigned long test = 13;
String thisString = String(test, HEX);
Serial.println(thisString);

The string constructors that take a base as the second argument do not appear to work as intended for unsigned long or unsigned int values.
Logged

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

You are right.  This is code from Arduino 0022
Code:
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 );
}

Notice
Code:
ultoa(value, buf, 10);

Should be
Code:
ultoa(value, buf, base);
« Last Edit: July 15, 2011, 09:39:27 pm by gentoouser » Logged

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

But to be honest, I don't think the String class should be constructible from integers anyway, especially since the constructors aren't declared as 'explicit'.

Code from WString.h
Code:
    String( const int, const int base=10);
    String( const unsigned int, const int base=10 );
    String( const long, const int base=10 );
    String( const unsigned long, const int base=10 );

This allows for implicit type conversion from an integer to a string:
Code:
String s;

// This can be confusing, making programmers
// think s is an integer. The integer 100 is converted
// to a String and then assigned to the variables s.
s = 100;

// The same thing happens with functions.
void foo(String s)
{
\\ ...
}

// The integer 100 is implicitly converted to a String.
foo(100);

// The function call foo(100) is the same as
foo(String(100));
Logged

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

Hello,

That was very helpful! I came across this bug today. So, is it possible to correct it? Is there a file a can change? Could you please explain the procedure?
Thanks
Logged

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

C:\Program Files (x86)\arduino-0022\hardware\arduino\cores\arduino\Wstring.cpp    line 91
Logged

Rob Tillaart

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

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

reported it at - http://code.google.com/p/arduino/issues/detail?id=577 -

Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: