Converting a hex string to its BigInteger equivalent negates the value

I have a string that is a large hexadecimal number that I want to convert to an integer. When I try to convert, the answer to the integer equivalent is negative. Here's the code:

string hex = "9782E78F1636";

BigInteger b1 = BigInteger.Parse(hex, NumberStyles.AllowHexSpecifier);

      

How can I get the correct result?

+3


source to share


3 answers


You need to add 0 to the line. While this number works with long.Parse

, with BigInt you get a negative number if the first digit is between 8-F

string hex = "09782E78F1636";
      BigInteger b1 = BigInteger.Parse(hex,NumberStyles.AllowHexSpecifier);
    Console.WriteLine(b1);

      


It's been a long time, but I feel like I had to explain why:



Signed integers use the most significant bit (MSB), which indicates whether the value is positive or negative. There's more to it than just msb, though .

Both BigInteger

and long

are integers: if the most significant number in the hexadecimal string is between 0 ( 0000

) and 7 ( 0111

), msb 0

, and the number is positive. If it is 8 ( 1000

) or higher, msb 1

and the number is negative.

long.Parse

knows the size of your parsing i.e. its hexadecimal representation 00009782E78F1636

. Msb 0

, so it's clean. Positive.

BigInt

doesn't have a fixed size like long. If you give it to him 9782E78F1636

, he doesn't know that you mean the same thing as00001782E78F1636

. He thinks the type is shorter. It expects a signed value and thinks that you count the first bit 9 ( 1001

) as an icon
. Adding a zero to the leading edge 09782E78F1636

allows you to understand that the MSL bit is equal 0

, and 1001

only a part of the actual value.

+6


source


Try the following:

var b1 = BigInteger.Parse("0" + hex, NumberStyles.HexNumber);

      



Don't forget that you need to add 0 to the sixth line. You will get a negative number if the first digit is between 8-F

+1


source


Your main character is between 8 and F, which means that it is negative as a signed number. This is not the case for BigInteger, it happens with all integer types. This is how they represent negative numbers in binary; using the most significant bit.

For most types, the location of the MSB depends on the size of that type, if you use all the bits, if the first character is between 8 and F, you get a negative number.

        string b = "91";
        SByte b1 = SByte.Parse(b, System.Globalization.NumberStyles.AllowHexSpecifier);
        Console.WriteLine("{0}={1}", b, b1);

        string s = "9123";
        Int16 s1 = Int16.Parse(s, System.Globalization.NumberStyles.AllowHexSpecifier);
        Console.WriteLine("{0}={1}", s, s1);

        string i = "91234567";
        Int32 i1 = Int32.Parse(i, System.Globalization.NumberStyles.AllowHexSpecifier);
        Console.WriteLine("{0}={1}", i, i1);

        string l = "9123456789012345";
        Int64 l1 = Int64.Parse(l, System.Globalization.NumberStyles.AllowHexSpecifier);
        Console.WriteLine("{0}={1}", l, l1);

      

BigInteger is not necessarily big, it is arbitrary in size and it will use enough bytes to match the number it gave to fill it with bytes and set the MSB so that it only negates the first character it encounters, If your number is unsigned, you need to add 0 to the beginning of the line.

        string big = "91";
        BigInteger big1 = BigInteger.Parse(big, System.Globalization.NumberStyles.AllowHexSpecifier);
        Console.WriteLine("{0}={1}", big, big1);

      

+1


source







All Articles