BigInteger.Parse() on hexadecimal number gives negative numbers.

Posted by brickner on Stack Overflow See other posts from Stack Overflow or by brickner
Published on 2010-06-06T09:30:53Z Indexed on 2010/06/06 9:32 UTC
Read the original article Hit count: 210

Filed under:
|
|
|

I've started using .NET 4 System.Numerics.BigInteger Structure and I've encountered a problem.

I'm trying to parse a string that contains a hexadecimal number with no sign (positive). I'm getting a negative number.

For example, I do the following two asserts:

Assert.IsTrue(System.Int64.Parse("8", NumberStyles.HexNumber, CultureInfo.InvariantCulture) > 0, "Int64");
Assert.IsTrue(System.Numerics.BigInteger.Parse("8", NumberStyles.HexNumber, CultureInfo.InvariantCulture) > 0, "BigInteger");

The first assert succeeds, the second assert fails. I actually get -8 instead of 8 in the BigInteger.

The problem seems to be when I'm the hexadecimal starts with 1 bit and not 0 bit (a digit between 8 and F inclusive). If I add a leading 0, everything works perfectly.

Is that a bad usage on my part? Is it a bug in BigInteger?

© Stack Overflow or respective owner

Related posts about parsing

Related posts about .net-4.0