4

Currently i am using Long integer type. I used the following to convert from/to binary/number:

Convert.ToInt64(BinaryString, 2); //Convert binary string of base 2 to number
Convert.ToString(LongNumber, 2); //Convert long number to binary string of base 2

Now the numbers i am using have exceeded 64 bit, so is started using BigInteger. I can't seem to find the equivalent of the code above.

How can i convert from a BinaryString that have over 64bits to a BigInteger number and vice versa ?

Update:

The references in the answer contains the answer i want but i am having some trouble in the conversion from Number to Binary.

I have used the following code which is available in the first reference:

    public static string ToBinaryString(this BigInteger bigint)
    {
        var bytes = bigint.ToByteArray();
        var idx = bytes.Length - 1;

        // Create a StringBuilder having appropriate capacity.
        var base2 = new StringBuilder(bytes.Length * 8);

        // Convert first byte to binary.
        var binary = Convert.ToString(bytes[idx], 2);

        // Ensure leading zero exists if value is positive.
        if (binary[0] != '0' && bigint.Sign == 1)
        {
            base2.Append('0');
        }

        // Append binary string to StringBuilder.
        base2.Append(binary);

        // Convert remaining bytes adding leading zeros.
        for (idx--; idx >= 0; idx--)
        {
            base2.Append(Convert.ToString(bytes[idx], 2).PadLeft(8, '0'));
        }

        return base2.ToString();
    }

The result i got is wrong:

100001000100000000000100000110000100010000000000000000000000000000000000 ===>  2439583056328331886592
2439583056328331886592 ===> 0100001000100000000000100000110000100010000000000000000000000000000000000

If you put the resulted binary string under each other, you will notice that the conversion is correct and that the problem is that there is a leading zero from the left:

100001000100000000000100000110000100010000000000000000000000000000000000
0100001000100000000000100000110000100010000000000000000000000000000000000

I tried reading the explanation provided in the code and changing it but no luck.

Update 2:

I was able to solve it by changing the following in the code:

        // Ensure leading zero exists if value is positive.
        if (binary[0] != '0' && bigint.Sign == 1)
        {
            base2.Append('0');

            // Append binary string to StringBuilder.
            base2.Append(binary);
        }
ykh
  • 1,775
  • 3
  • 31
  • 57

2 Answers2

5

Unfortunately, there is nothing built-in in the .NET framework.

Fortunately, the StackOverflow community has already solved both problems:

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
0

There is a good reference on MSDN about BigIntegers. Can you check it? https://msdn.microsoft.com/en-us/library/system.numerics.biginteger(v=vs.110).aspx

Also there is a post to convert from binary to biginteger Conversion of a binary representation stored in a list of integers (little endian) into a Biginteger

This example is from MSDN.

string positiveString = "91389681247993671255432112000000";
string negativeString = "-90315837410896312071002088037140000";
BigInteger posBigInt = 0;
BigInteger negBigInt = 0;

try {
   posBigInt = BigInteger.Parse(positiveString);
   Console.WriteLine(posBigInt);
}
catch (FormatException)
{
   Console.WriteLine("Unable to convert the string '{0}' to a BigInteger value.", 
                     positiveString);
}

if (BigInteger.TryParse(negativeString, out negBigInt))
  Console.WriteLine(negBigInt);
else
   Console.WriteLine("Unable to convert the string '{0}' to a BigInteger value.", 
                      negativeString);

// The example displays the following output:
//   9.1389681247993671255432112E+31
//   -9.0315837410896312071002088037E+34
Community
  • 1
  • 1
Ugur Basak
  • 317
  • 4
  • 9
  • 1
    Those are not strings of binary digits. The question is pretty specific to dealing with "string of base 2" – Ben Voigt Nov 09 '15 at 15:08
  • 1
    There is an answer in this post, is this what you want? http://stackoverflow.com/questions/22947412/conversion-of-a-binary-representation-stored-in-a-list-of-integers-little-endia – Ugur Basak Nov 09 '15 at 15:13