C# Convert large binary string to decimal system

前端 未结 1 755
离开以前
离开以前 2021-01-07 03:39

I\'ve got a large text, containing a number as a binary value. e.g \'123\' would be \'001100010011001000110011\'. EDIT: should be 1111011

Now I want to convert it to

1条回答
  •  萌比男神i
    2021-01-07 04:12

    This'll do the trick:

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

    0 讨论(0)
提交回复
热议问题