Lennart
Lennart

Reputation: 345

Convert large binary string to decimal string

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 the decimal system, but the number is too large for Int64.

So, what I want: Convert a large binary string to decimal string.

Upvotes: 3

Views: 5373

Answers (1)

Nuffin
Nuffin

Reputation: 3972

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

Upvotes: 16

Related Questions