将BigInteger二进制转换为BigInteger数字?

时间:2015-11-09 15:00:12

标签: c# binary biginteger

目前我正在使用Long整数类型。我使用以下内容从/转换为二进制/数字:

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

现在我使用的数字已超过64位,因此使用BigInteger开始。我似乎找不到上面代码的等价物。

如何将具有超过64位的BinaryString转换为BigInteger数,反之亦然?

更新

答案中的引用包含我想要的答案,但我在从Number转换为Binary时遇到了一些麻烦。

我使用了第一个参考中提供的以下代码:

    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();
    }

我得到的结果是错误的:

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

如果你把结果二进制字符串放在彼此之下,你会注意到转换是正确的,问题是左边有一个前导零:

100001000100000000000100000110000100010000000000000000000000000000000000
0100001000100000000000100000110000100010000000000000000000000000000000000

我尝试阅读代码中提供的解释并更改但没有运气。

更新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);
        }

2 个答案:

答案 0 :(得分:5)

不幸的是,.NET框架中没有任何内置功能。<​​/ p>

幸运的是,StackOverflow社区已经解决了这两个问题:

答案 1 :(得分:-1)

关于BigIntegers的MSDN有一个很好的参考。你能检查一下吗? https://msdn.microsoft.com/en-us/library/system.numerics.biginteger(v=vs.110).aspx

还有一个帖子可以从二进制转换为biginteger Conversion of a binary representation stored in a list of integers (little endian) into a Biginteger

此示例来自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
相关问题