How to convert binary to decimal

Viewed 85120

How can I convert a binary string, such as 1001101 to Decimal? (77)

11 Answers

A manual way to convert a binary string to an uint using binary or and shift could be:

  public static uint ConvertBinaryStringToUInt32(string binaryString)
  {
     if (binaryString is null)
     {
        throw new ArgumentNullException(nameof(binaryString));
     }

     if (binaryString.Length > 32)
     {
        throw new ArgumentOutOfRangeException(nameof(binaryString), binaryString.Length, "The specified binary string can not be longer than 32 characters.");
     }

     uint result = 0u;

     for (int i = 0; i < binaryString.Length; i++)
     {
        result <<= 1;

        char c = binaryString[i];

        if (c == '0')
        {
        }
        else if (c == '1')
        {
           result |= 1u;
        }
        else
        {
           throw new FormatException($"Character {i} of binary string \"{binaryString}\" is an invalid '{c}'. Can only be '0' or '1'.");
        }
     }

     return result;
  }

// 1. In the first method we will use parseInt() function to convert binary to decimal..
        
        let binaryDigit = 11011; let getDecimal = parseInt(binaryDigit, 2);
        console.log(getDecimal);
        

   

<!-- begin snippet: js hide: false console: true babel: false -->

Check my github repo to find same kind of logicle puzzles

I wanted a solution that always gave 32 bits no matter how big or small the number. So this is what I created.

public static string ConvertUintToBitString(uint Number)
{
    string _BitString = string.Empty;

    if (Number >= 2147483648)
    {
        _BitString += '1';
        Number = Number - 2147483648;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 1073741824)
    {
        _BitString += '1';
        Number = Number - 1073741824;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 536870912)
    {
        _BitString += '1';
        Number = Number - 536870912;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 268435456)
    {
        _BitString += '1';
        Number = Number - 268435456;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 134217728)
    {
        _BitString += '1';
        Number = Number - 134217728;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 67108864)
    {
        _BitString += '1';
        Number = Number - 67108864;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 33554432)
    {
        _BitString += '1';
        Number = Number - 33554432;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 16777216)
    {
        _BitString += '1';
        Number = Number - 16777216;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 8388608)
    {
        _BitString += '1';
        Number = Number - 8388608;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 4194304)
    {
        _BitString += '1';
        Number = Number - 4194304;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 2097152)
    {
        _BitString += '1';
        Number = Number - 2097152;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 1048576)
    {
        _BitString += '1';
        Number = Number - 1048576;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 524288)
    {
        _BitString += '1';
        Number = Number - 524288;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 262144)
    {
        _BitString += '1';
        Number = Number - 262144;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 131072)
    {
        _BitString += '1';
        Number = Number - 131072;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 65536)
    {
        _BitString += '1';
        Number = Number - 65536;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 32768)
    {
        _BitString += '1';
        Number = Number - 32768;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 16384)
    {
        _BitString += '1';
        Number = Number - 16384;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 8192)
    {
        _BitString += '1';
        Number = Number - 8192;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 4096)
    {
        _BitString += '1';
        Number = Number - 4096;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 2048)
    {
        _BitString += '1';
        Number = Number - 2048;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 1024)
    {
        _BitString += '1';
        Number = Number - 1024;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 512)
    {
        _BitString += '1';
        Number = Number - 512;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 256)
    {
        _BitString += '1';
        Number = Number - 256;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 128)
    {
        _BitString += '1';
        Number = Number - 128;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 64)
    {
        _BitString += '1';
        Number = Number - 64;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 32)
    {
        _BitString += '1';
        Number = Number - 32;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 16)
    {
        _BitString += '1';
        Number = Number - 16;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 8)
    {
        _BitString += '1';
        Number = Number - 8;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 4)
    {
        _BitString += '1';
        Number = Number - 4;
    }
    else
    {
        _BitString += '0';
    }
    if (Number >= 2)
    {
        _BitString += '1';
        Number = Number - 2;
    }
    else
    {
        _BitString += '0';
    }
    if (Number == 1)
    {
        _BitString += '1';
    }
    else
    {
        _BitString += '0';
    }

    return _BitString;
}

I have tried this after reading your problem. It is a bit longer but it provides a solution. I saved binary elements in an array to get a solution. Like I said It is a bit longer, much shorter ways can be found.

        // Binary ------> Decimal 
        int len;
        double deci = 0;

        Console.Write("Length of binary number: ");
        len = Convert.ToInt32(Console.ReadLine());

        int[] bnry = new int[len];

        for (int i = 0; i < len; i++)
        {
            Console.Write("{0} index of binary number: ", i);
            bnry[i] = Convert.ToInt32(Console.ReadLine());
        }
        Console.Write("Your binary number: ");
        for (int i = len - 1; 0 <= i; i--)
        {

            Console.Write(bnry[i]);

        }

        Console.Write("\nDecimal number: ");
        for (int i = 0; i < len; i++)
        {
            deci += (bnry[i] * Math.Pow(2, i));
        }
        Console.Write(deci);

I just made an other manual way

private int binary2Decimal(string bin)
    {
        int res;
        char[] cArr = bin.ToCharArray();
        Array.Reverse(cArr); // Reverse binary string
        List<int> iArr = new List<int>();
        for (int i = bin.Length - 1; i > -1; i--) // Get the bits
            iArr.Add(Int16.Parse(cArr[i].ToString()) * (int)Math.Pow(2, i)); // Calculate each bits and add to an array
        res = iArr.ToArray().Sum(); // Calculate all the numbers and add to the final result
        return res;
    }
public static int ToDecimal(int n) //1110
    {
        int result = 0;
        var number = n.ToString().ToCharArray();
        var dic = new Dictionary<int, string>();
        var len = number.Length - 1;

        for (int i = 0; i < number.Length && len >= 0; i++)
        {
            dic.Add(i, number[len--].ToString());
        }

        foreach (var item in dic)
        {
            if(Convert.ToInt32(item.Value) > 0 )
            {
                result = result + Convert.ToInt32(Math.Pow(2, item.Key));
            }
        }

        return result;
        
    }

This should work

        static int binaryToDecimal(string input) {
         int result = 0;
         for (int i = input.Length; i > 0 ; i--)
             if (input.Substring(i-1,1) == "1")
                 result += (int)Math.Pow(2,(input.Length-i));
         return result;
        }
Related