Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse 0x8 and -0x8 correctly #194

Merged
merged 1 commit into from
Mar 1, 2022
Merged

Parse 0x8 and -0x8 correctly #194

merged 1 commit into from
Mar 1, 2022

Conversation

lynn
Copy link
Contributor

@lynn lynn commented Feb 17, 2022

A nasty hidden behavior of BigInteger.TryParse was making 0x8 parse as -8:

If value is a hexadecimal string, the TryParse(String, NumberStyles, IFormatProvider, BigInteger) method interprets value as a negative number stored by using two's complement representation if its first two hexadecimal digits are greater than or equal to 0x80. In other words, the method interprets the highest-order bit of the first byte in value as the sign bit. To make sure that a hexadecimal string is correctly interpreted as a positive number, the first digit in value must have a value of zero.

This PR makes 0x8 parse as 8, and -0x8 (previously unsupported) parse as -8.

@lynn lynn requested a review from emily33901 February 17, 2022 21:57
Copy link

@emily33901 emily33901 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@lynn lynn merged commit cce4938 into master Mar 1, 2022
@lynn lynn mentioned this pull request Mar 11, 2022
@andrewmd5 andrewmd5 deleted the parse-positive-hex branch July 18, 2022 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants