【问题标题】:Convert BigInteger Binary to BigInteger Number?将 BigInteger 二进制转换为 BigInteger 数?
【发布时间】:2015-11-09 15:00:11
【问题描述】:

目前我正在使用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


    【解决方案1】:

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

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

    【讨论】:

    • 我更新了问题。请看看我面临的问题
    【解决方案2】:

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

    还有一篇文章将二进制转换为大整数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
    

    【讨论】:

    猜你喜欢
    • 2013-07-23
    • 1970-01-01
    • 2019-03-08
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    • 2018-12-12
    • 1970-01-01
    相关资源
    最近更新 更多