Thread overview
[Issue 9797] to!int() cannot convert hexadecimal numbers
Nov 06, 2015
Cauterite
Nov 07, 2015
bb.temp@gmx.com
Apr 02, 2016
b2.temp@gmx.com
Apr 02, 2016
Jonathan M Davis
Feb 08, 2020
Basile-z
Mar 21, 2020
Basile-z
November 06, 2015
https://issues.dlang.org/show_bug.cgi?id=9797

Cauterite <cauterite@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |cauterite@gmail.com

--- Comment #3 from Cauterite <cauterite@gmail.com> ---
I can see only one reasonable(ish) solution here:
Patch toImpl() to detect prefixes like "0x" and "0b", so it calls parse() with
the appropriate radix. This may not be as much of a problem as patching parse()
itself, since toImpl() either parses the whole input successfully or throws.

However, if the caller is not fully aware that to() accepts these alternative
number syntaxes, it could easily cause subtle bugs. Additionally, having
parse() and to() operate differently doesn't sit right with me.

The only other option I can think of would be to provide a separate function specifically for parsing D-style numeric literals, but this is out of the scope of std.conv

Unless anyone has a better idea, I suggest WONTFIX. It's not an *especially* common problem, and parse() with explicit radix makes it trivial for callers to deal with it themselves.

--
November 07, 2015
https://issues.dlang.org/show_bug.cgi?id=9797

bb.temp@gmx.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bb.temp@gmx.com

--- Comment #4 from bb.temp@gmx.com ---
Agree, furthemore there are others prefix/suffix notations:

- 12345678h: several hex editors , IDA
- #12345678: colors, html, css
- $12345678: Pascal
- 0X or 0x
- and maybe others...

so even if 0x is handled ,there are several cases where the user has to parse the  prefix/suffix by hand anyway.

--
April 02, 2016
https://issues.dlang.org/show_bug.cgi?id=9797

b2.temp@gmx.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |WONTFIX
           Severity|major                       |enhancement

--- Comment #5 from b2.temp@gmx.com ---
> In D a hexadecimal number is prefixed with 0x. Not being able to recognize a valid D hexadecimal number is a bug in to!int().

And then even if done, someone will come and report that `to()` is not able to
convert "0x1_2_3_4_P5" (valid D hexadecimal litteral)!
So your argument is invalid.

--
April 02, 2016
https://issues.dlang.org/show_bug.cgi?id=9797

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

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

--- Comment #6 from Jonathan M Davis <issues.dlang@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.

--
February 08, 2020
https://issues.dlang.org/show_bug.cgi?id=9797

Basile-z <b2.temp@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://issues.dlang.org/sh
                   |                            |ow_bug.cgi?id=20568

--
March 21, 2020
https://issues.dlang.org/show_bug.cgi?id=9797

Basile-z <b2.temp@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|b2.temp@gmx.com             |

--