C#将大二进制字符串转换为十进制系统

时间:2012-01-07 23:43:28

标签: c#

我有一个大文本,包含一个数字作为二进制值。例如'123'将是'001100010011001000110011'。编辑:应该是1111011

现在我想将它转换为十进制系统,但这个数字对于int64来说太大了。

那么,我想要的是:将一个大的二进制字符串转换为十进制字符串。

1 个答案:

答案 0 :(得分:12)

这就是诀窍:

public string BinToDec(string value)
{
    // BigInteger can be found in the System.Numerics dll
    BigInteger res = 0;

    // I'm totally skipping error handling here
    foreach(char c in value)
    {
        res <<= 1;
        res += c == '1' ? 1 : 0;
    }

    return res.ToString();
}
相关问题