www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - char [] toString(char x )

reply Charlie <Charlie_member pathlink.com> writes:
In std.string toString(char x ) is :

char[] toString(char c)
{
char[] result = new char[2];
result[0] = c;
result[1] = 0;
return result;
}


Whats with that null char ?  Should this be toStringz ?

Anyway this is failing :

char [] f = "Fizzle";
char [] newF;
foreach ( char x;f ) {
newF ~= std.string.toString(x);
}
printf("%.*s",newF);

Only prints 'F'

however this works 

char [] f = "Fizzle";
char [] newF;
foreach ( char x;f ) {
newF ~= std.string.toString(x)[0 .. 1];
}
printf("%.*s",newF);



Thanks,
C
May 18 2004
parent "Vathix" <vathixSpamFix dprogramming.com> writes:
"Charlie" <Charlie_member pathlink.com> wrote in message
news:c8dr4m$24oe$1 digitaldaemon.com...
 In std.string toString(char x ) is :

 char[] toString(char c)
 {
 char[] result = new char[2];
 result[0] = c;
 result[1] = 0;
 return result;
 }
I'm guessing it was done that way to make it easier to use with C functions: toStringz() wouldn't need to copy it; but whoever wrote it forgot to just return a slice of the first char. I didn't even know that function existed. Obvious fix that needs to be made: char[] toString(char c) { char[] result = new char[2]; result[0] = c; result[1] = 0; return result[0 .. 1]; }
May 26 2004