Thread overview
std.string.toString(int x < 9) returns funny results
Sep 26, 2004
ajvincent
Sep 26, 2004
Burton Radons
Sep 26, 2004
ajvincent
Sep 27, 2004
J C Calvarese
Re: std.string.toString(int x < 9) returns funny results - testcase.d
Sep 26, 2004
ajvincent
Sep 26, 2004
ajvincent
Sep 26, 2004
Sean Kelly
Sep 27, 2004
Toaster
Sep 28, 2004
Batman
September 26, 2004
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...


September 26, 2004
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);
September 26, 2004
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.


September 26, 2004
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


September 26, 2004
Oh, don't I feel stupid.  Sorry!


September 26, 2004
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
>
>


September 27, 2004
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/
September 27, 2004
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.


September 28, 2004
On Sun, 26 Sep 2004 23:26:26 +0000 (UTC), ajvincent@juno.com wrote:

>char[] iString;
>printf(iString);

Surely it should be:

#   printf("%.*s", iString);

It's all in the FAQ at http://www.digitalmars.com/d/faq.html#printf. Jill