digitalmars.D - std.string.toString(int x < 9) returns funny results
- ajvincent juno.com (9/9) Sep 26 2004 Lines 1814-1816 give some really weird results when you call the toStrin...
- Burton Radons (6/18) Sep 26 2004 Check the FAQ:
- ajvincent juno.com (1/1) Sep 26 2004 Oh, don't I feel stupid. Sorry!
- J C Calvarese (7/8) Sep 26 2004 You're not the first one to run into a problem like this. A lengthy
- ajvincent juno.com (36/36) Sep 26 2004 Testcase attached. When I run from the terminal, I expect:
- ajvincent juno.com (44/44) Sep 26 2004 Testcase:
- Sean Kelly (3/47) Sep 26 2004 try:
- Toaster (12/14) Sep 27 2004 AFAIK you cannot call printf with a dynamic char array as argument, it
- Batman (5/7) Sep 28 2004 Surely it should be:
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
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
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
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
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
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
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