www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.string.toString(int x < 9) returns funny results

reply ajvincent juno.com writes:
Lines 1814-1816 give some really weird results when you call the toString()
method with a value less than 9.

For instance, if I say:

byte x = 2;
char[] y = string.toString(x);
printf(y);

I get back:

"23456789"

That can not be right...
Sep 26 2004
next sibling parent reply Burton Radons <burton-radons shaw.ca> writes:
ajvincent juno.com wrote:

 Lines 1814-1816 give some really weird results when you call the toString()
 method with a value less than 9.
 
 For instance, if I say:
 
 byte x = 2;
 char[] y = string.toString(x);
 printf(y);
 
 I get back:
 
 "23456789"
Check the FAQ: http://www.digitalmars.com/d/faq.html#printf Try not to use printf; you can use writef instead: import std.stdio; writef (x);
Sep 26 2004
parent reply ajvincent juno.com writes:
Oh, don't I feel stupid.  Sorry!
Sep 26 2004
parent J C Calvarese <jcc7 cox.net> writes:
ajvincent juno.com wrote:
 Oh, don't I feel stupid.  Sorry!
You're not the first one to run into a problem like this. A lengthy discussion on the topic is available here: http://www.prowiki.org/wiki4d/wiki.cgi?HowTo/printf -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Sep 26 2004
prev sibling next sibling parent ajvincent juno.com writes:
Testcase attached.  When I run from the terminal, I expect:

0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9

I actually get:
0 0123456789
1 123456789
2 23456789
3 3456789
4 456789
5 56789
6 6789
7 789
8 89
9 9

Tested on Linux operating system using D compiler ported to gcc.


begin 0644 testcase.d




M.PT*("` ("` :5-T<FEN9RYL96YG=&  /2`Q.PT*("` ("` :5-T<FEN9ULP

M<FEN=&8H(B`B*3L-"B` ("` (`T*("` ("` :5-T<FEN9R`]('-T<FEN9RYT
M;U-T<FEN9RAI*3L-"B` ("` ('!R:6YT9BAI4W1R:6YG*3L-"B` ("` ('!R
M:6YT9B B7&XB*3L-"B` ("` (`T*("` ("` :5-T<FEN9R`]("(B.PT*("` 
6('T-"B` ("!R971U<FX ,#L-"GT-"B` 
`
end
Sep 26 2004
prev sibling parent reply ajvincent juno.com writes:
Testcase:

import string;
import conv;

int main(char[][] args)
{
ubyte i;
char[] iString;
char jString;

for (i = 0; i < 10; i++) {
jString = string.digits[i];
iString.length = 1;
iString[0] = jString;
printf(iString);
printf(" ");

iString = string.toString(i);
printf(iString);
printf("\n");

iString = "";
}
return 0;
}

Using ftp://ftp.digitalmars.com/dmd.zip under Linux:

Expected output:
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9

Actual output:
0 0123456789
1 123456789
2 23456789
3 3456789
4 456789
5 56789
6 6789
7 789
8 89
9 9
Sep 26 2004
next sibling parent Sean Kelly <sean f4.ca> writes:
In article <cj7j72$qub$1 digitaldaemon.com>, ajvincent juno.com says...
Testcase:

import string;
import conv;

int main(char[][] args)
{
ubyte i;
char[] iString;
char jString;

for (i = 0; i < 10; i++) {
jString = string.digits[i];
iString.length = 1;
iString[0] = jString;
printf(iString);
try: printf("%.*s", iString);
printf(" ");

iString = string.toString(i);
printf(iString);
printf("\n");

iString = "";
}
return 0;
}

Using ftp://ftp.digitalmars.com/dmd.zip under Linux:

Expected output:
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9

Actual output:
0 0123456789
1 123456789
2 23456789
3 3456789
4 456789
5 56789
6 6789
7 789
8 89
9 9
Sep 26 2004
prev sibling parent reply Toaster <wb sapo.pt> writes:
On Sun, 26 Sep 2004 23:26:26 +0000 (UTC), ajvincent juno.com wrote:

char[] iString;
[...]
printf(iString);
AFAIK you cannot call printf with a dynamic char array as argument, it needs a char* as it did before. There is a function to get a \0 terminated char* for a char[] array: char* iStringz = std.string.toStringz(iString); I did the same but for me it segfaulted (i think a char array does not necessarily have a terminating \0). I ran into this when I started with D and I think it is easy to get it wrong, since it compiles just fine. You get used to it quickly. Just don't forget a dynamic array and a char pointer are two different things in D.
Sep 27 2004
parent Batman <Batman_member pathlink.com> writes:
On Sun, 26 Sep 2004 23:26:26 +0000 (UTC), ajvincent juno.com wrote:

char[] iString;
printf(iString);
Surely it should be: It's all in the FAQ at http://www.digitalmars.com/d/faq.html#printf. Jill
Sep 28 2004