Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#将大二进制字符串转换为十进制_C# - Fatal编程技术网

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

C#将大二进制字符串转换为十进制,c#,C#,我有一个大文本,包含一个作为二进制值的数字。e、 g‘123’将是‘0011000110011’。编辑:应为1111011 现在我想把它转换成十进制,但是这个数字对于int64来说太大了 所以,我想要的是:将一个大的二进制字符串转换为十进制字符串。这将实现以下技巧: public string BinToDec(string value) { // BigInteger can be found in the System.Numerics dll BigInteger res

我有一个大文本,包含一个作为二进制值的数字。e、 g‘123’将是‘0011000110011’。编辑:应为1111011

现在我想把它转换成十进制,但是这个数字对于int64来说太大了

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

这将实现以下技巧:

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();
}
公共字符串BinToDec(字符串值)
{
//可以在System.Numerics dll中找到BigInteger
大整数res=0;
//我完全跳过了错误处理
foreach(字符c值)
{

我的计算器说1100010011001000110011₂ 等于3224115₁₀, 不是123₁₀. 3224115₁₀ 对于Int64来说不是太大。你用谷歌搜索过吗?我发现了:你是如何从123十进制到001100011001000110011二进制的?闻起来像是家庭作业,谁会读到>20位数字?没有家庭作业,我用谷歌在ascii上搜索123到bin。哦,这些数字代表文件内容。编辑:123应该是1111011OP说它对Int64来说太大了,所以他想要一个十进制字符串。是的,将其更改为符合他的需要。BigInteger在System.Numerics中,默认情况下未被引用。@MikeW:这就是为什么在您键入注释时我在示例中插入了位置;)