Convert Binary into Decimal in C#

In this article, learn how to convert binary into decimal in C#. The following C# code snippet explain how to convert binary values into decimal. Also, do find more c# examples here for your reference.


Convert Binary into Decimal in C# (normal):

Here is the function to convert binary into decimal in c#. The function takes one input parameter where the binary value to be converted into decimal is passed as string to this function. The function return the decimal value for the given binary input and return value -1 incase any exception or errors.


// Function to convert Binary number to Decimal.
// Parameter: The binary value as string.
// Returns: The decimal number.
public static long BinaryToDecimal(string binValue)
{
    long decVal = 0;
    long baseVal = 1;
    long rem;
    long binVal = 0;

    bool isParseError = false;

    try
    {
        binVal = long.Parse(binValue);
    }
    catch(Exception ex)
    {
        //Possibily ArgumentNullException, OverflowException, FormatException
        isParseError = true;
        decVal = -1;
    }

    if (!isParseError)
    {
        //Convert Binary to Decimal.
        while (binVal > 0)
        {
            rem = binVal % 10;
            decVal = decVal + rem * baseVal;
            binVal = binVal / 10;
            baseVal = baseVal * 2;
        }
    }

    return decVal;
}


Try yourself:

Here is the full program and you can also execute this program and see the results. You can also modify the input and get the converted decimal output.

Convert Binary into Decimal in C# (With Math class):

Here is the function to convert binary into decimal in c#. The function takes one input parameter where the binary value to be converted into decimal is passed as a string to this function. The function returns the decimal value for the given binary input.

.

// Convert Binary into Decimal using Math function in C#.
// Parameter: The binary value in string
// Returns: The decimal value
public static int BinaryToDecimalMath(string binValue)
{
    char[] array = binValue.ToCharArray();

    // Reverse since 16-8-4-2-1 not 1-2-4-8-16. 
    Array.Reverse(array);
    /*
        * [0] = 1
        * [1] = 2
        * [2] = 4
        * etc
        */
    int decVal = 0;

    for (int i = 0; i < array.Length; i++)
    {
        if (array[i] == '1')
        {
            // Method uses raising 2 to the power of the index. 
            if (i == 0)
            {
                decVal += 1;
            }
            else
            {
                decVal += (int)Math.Pow(2, i);
            }
        }

    }

    return decVal;
}

Try yourself:

Here is the full program and you can also execute this program and see the results. You can also modify the input and get the converted decimal output.



- Article ends here -

If you have any questions, please feel free to share your questions or comments on the comment box below.

Share this:
We will be happy to hear your thoughts

      Leave a reply

      www.troubleshootyourself.com
      Logo