Trivial: Binary To Integer

In response to a web-forum question, I found myself writing a little C# to convert from a string (containing a binary number) into the integer representation of the same. It’s the kind of trivial coding problem that first year programmers would come up against… Of course, we could make it harder by stipulating that we cannot use System.Convert.

I’m sure that there are much better ways of doing this, and I’d be glad to see some of them appear in the comments for this post. There, a kind of challenge…If the quality of the comments is good enough, I might be able to offer a small prize if you are a UK resident. No promises though!

[code lang=”C#”]
public int BinToInt(string binaryNumber)
{
int multiplier = 1;
int converted = 0;

for (int i = binaryNumber.Length – 1; i >= 0; i–)
{
int t = System.Convert.ToInt16(binaryNumber[i].ToString());
converted = converted + (t * multiplier);
multiplier = multiplier * 2;
}
return converted;
}
[/code]

In use:

[code lang=”C#”]
// remember SEFTU, sixteen, eight, four, two, one
listBox1.Items.Add(BinToInt(“00001”).ToString()); // 1
listBox1.Items.Add(BinToInt(“00010”).ToString()); // 2
listBox1.Items.Add(BinToInt(“00011”).ToString()); // 3
listBox1.Items.Add(BinToInt(“00100”).ToString()); // 4
listBox1.Items.Add(BinToInt(“00101”).ToString()); // 5
listBox1.Items.Add(BinToInt(“00111”).ToString()); // 7
listBox1.Items.Add(BinToInt(“01000”).ToString()); // 8
listBox1.Items.Add(BinToInt(“10000”).ToString()); // 16[/code]

It might be of use to somebody…somewhere…

6 thoughts on “Trivial: Binary To Integer”

  1. How about doing this recusrively? – this would avoid the need for the multiplier and converted variables, but would probably in practice be less efficient.

    private int BinToInt(String s)
    {
    int lastnumber = s[s.Length-1] == ‘1’ ? 1 : 0;
    if (s.Length > 1)
    {
    return 2 * BinToInt(s.Substring(0,s.Length – 1)) + lastnumber;
    }
    else
    {
    return lastnumber;
    }
    }

  2. It would be much faster to use bitwise operators… I assume that we are going to have the string not 2’s complement encoded, so that this is always a positive number. Also, real world would do a length check to avoid overflow exceptions…

    public static System.UInt32 BinToInt(string binaryNumber)
    {
    UInt32 converted = 0;
    for( int i = binaryNumber.Length – 1; i >= 0; i–)
    if( binaryNumber[i] == ‘1’ ) converted = converted | (((UInt32)1)

  3. Oops, that cut off some code, try again:

    public static System.UInt32 BinToInt(string binaryNumber)
    {
    UInt32 converted = 0;
    for( int i = binaryNumber.Length – 1; i >= 0; i–)
    if( binaryNumber[i] == ‘1’ ) converted = converted | (((UInt32)1) << (binaryNumber.Length-1-i));
    return converted;
    }

  4. How about using System.Convert to convert a base 2 string to an unsigned integer? Something ala

    Convert.ToByte(“101”, 2); // 101 (base2) = 5 (base10)

    To print it out of course you’d use Convert.ToByte(“101”, 2).ToString() ….

  5. int number = Convert.ToInt32(binary_string,2);
    “000101” => 5

Comments are closed.