Bug 18290 – std.conv.parse throws ConvOverflowException for negative values in hex

Status
REOPENED
Severity
trivial
Priority
P4
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-01-24T00:00:48Z
Last change time
2024-12-01T16:32:10Z
Keywords
bootcamp
Assigned to
No Owner
Creator
Răzvan Ștefănescu
Moved to GitHub: phobos#10296 →

Comments

Comment #0 by rumbu — 2018-01-24T00:00:48Z
string input = "80000000"; auto x = parse!int(input, 16); //ConvOverflowException
Comment #1 by tiberiulepadatu14 — 2018-10-21T13:40:24Z
(In reply to Răzvan Ștefănescu from comment #0) > string input = "80000000"; > auto x = parse!int(input, 16); > //ConvOverflowException I feel that this is not a bug. To show that you are using a hex string you must use "0x" in front. If you write "0x80000000" instead of "80000000" there is no bug.
Comment #2 by rumbu — 2018-10-22T09:12:10Z
(In reply to Tiberiu Lepadatu from comment #1) > (In reply to Răzvan Ștefănescu from comment #0) > > string input = "80000000"; > > auto x = parse!int(input, 16); > > //ConvOverflowException > > I feel that this is not a bug. To show that you > are using a hex string you must use "0x" in front. > If you write "0x80000000" instead of "80000000" > there is no bug. parsing 0x80000000 will read only the "0" before x, which is not the intended behavior, therefore x will become 0, not -2147483648. In order to show that I am using a hex string, I specify the radix (16). To go further, how do I parse a string with radix 7? How do I prefix the string?
Comment #3 by bugzilla — 2019-12-14T12:28:02Z
80000000(16) = 2147483648(10) which is larger than int.max. Therefore the exception is correct. If you want to have -214748364, just add a minus sign in the string.
Comment #4 by rumbu — 2019-12-16T10:59:38Z
to!string(int.min, 16) returns "80000000" So, please tell me, which is the hex representation of int.min (-2147483648)? Where do I put the "-" sign? Because, you know, +2147483648 is not an integer, it's a long. Just to play dumb, taking your advice to put a "-" before: parse!int("-80000000", 16) returns 0 :)
Comment #5 by bugzilla — 2019-12-16T12:06:10Z
(In reply to Răzvan Ștefănescu from comment #4) > Just to play dumb, taking your advice to put a "-" before: > parse!int("-80000000", 16) returns 0 :) OK, thanks. This makes for a much better test: --- void main() { import std.conv; string s = "-80000000"; assert(parse!int(s,16) == int.min); } --- Currently the assertion fails. A similar problem arises with to!int("-80000000,16), which produces an Exception. With the radix 10 this works, all other radices fail.
Comment #6 by rumbu — 2019-12-16T14:48:03Z
In my opinion, you are mixing decimal math with two's complement math. Having a sign before a hexadecimal representation which already has the sign bit set is pure nonsense. Conversion between string and integrals (and vice versa) must render exact results. As long as to!string(int.min, 16) returns "80000000", I expect that parse!int("80000000", 16) to return int.min. In conclusion, please keep my original issue as is, if you want negative hex numbers, open another issue, don't adjust that one. Thanks.
Comment #7 by bugzilla — 2019-12-16T16:16:00Z
(In reply to Răzvan Ștefănescu from comment #6) > As long as to!string(int.min, 16) returns "80000000", I expect that > parse!int("80000000", 16) to return int.min. Looks like an other bug. > In conclusion, please keep my original issue as is, if you want negative hex > numbers, open another issue, don't adjust that one. OK. But IMHO it's invalid and should be closed then.
Comment #8 by robert.schadek — 2024-12-01T16:32:10Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/10296 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB