Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Convert ulong to string

Moderators: larsivi kris

Posted: 04/05/09 22:38:28

I can't seem to find way to convert a ulong to a string, without loosing some data. I tried using tango.text.convert.Integer.toString(). But that casts to long.

It was talked about in this bug report, but it seems to ignore the problem: http://www.dsource.org/projects/tango/ticket/1460

It does not matter how inefficient or verbose the code is. Any way to do it would be appreciated.

Author Message

Posted: 04/05/09 23:01:08

Oops

I found the answer. There is a format argument for toString. You can specify unsigned as the format. I didn't realise that was what was said in the bug report mentioned in the initial post.

tango.text.convert.Integer.toString(ulong.max, "u")

Posted: 04/06/09 00:09:43

Interesting. While that was working. The compiler in maximum complain mode (gdc -Wall -Wextra -Wshadow -Werror -pedantic-errors) shows that the tango.text.convert.Integer function takes a long for its first argument. So even though it works, it is still casting. So it will not work in my case.

Instead I looked at the source code to the function: http://dsource.org/projects/tango/docs/stable/htmlsrc/tango.text.convert.Integer.html#L93

and just did the same thing, but without the casting:

public static char[] to_s(ulong value) {
    char[66] tmp = void;
    return tango.text.convert.Integer.format(tmp, value, "u").dup;
}

I hope this will be helpfully to anyone, that has been banging their head against this too.