在 C# 中将 BigInteger 二进制转换为 BigInteger 数字?

问题描述 投票:0回答:2

目前我正在使用

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 数字,反之亦然?

更新:

答案中的参考文献包含我想要的答案,但我在从数字到二进制的转换时遇到了一些麻烦。

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

    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);
        }
c# binary biginteger
2个回答
6
投票

不幸的是,.NET 框架中没有内置任何东西。

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


0
投票

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

还有一篇文章将二进制转换为大整数将存储在整数列表(小端)中的二进制表示形式转换为大整数

这个例子来自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
© www.soinside.com 2019 - 2024. All rights reserved.