[Issue 9797] to!int() cannot convert hexadecimal numbers

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sat Apr 2 08:57:26 PDT 2016


https://issues.dlang.org/show_bug.cgi?id=9797

Jonathan M Davis <issues.dlang at jmdavisProg.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |issues.dlang at jmdavisProg.co
                   |                            |m

--- Comment #6 from Jonathan M Davis <issues.dlang at jmdavisProg.com> ---
I would point out that while to!int("0x123"); isn't going to work,
to!int("123", 16); will. e.g.

    import std.conv;

    void main()
    {
        auto str = to!string(42, 16);
        assert(str == "2A", str);
        auto i = to!int(str, 16);
        assert(i == 42);
    }

So, if you check for 0x and strip it off before feeding the string to to!int,
then it will do the conversion. It just won't work with the prefix on it. And
while having to!int check for the prefix as well as anything else that's valid
in a numeric literal in D might be nice in some cases, it would slow down
to!int for everything else, which really isn't a good tradeoff given how much
to!int is used. A conversion function specifically for literals might make
sense, but I have to concur that doing it with std.conv.to isn't worth it.

--


More information about the Digitalmars-d-bugs mailing list