Converting binary to decimal

This time you will learn how to convert binary to decimal. Again, you can use the online converter to test your results. Then you will learn how to create a similar program in C. I prepared an example implementation and you can download its source code below.



Binary to decimal converter

Input the binary value The decimal number is

The conversion algorithm

If you don't know about the different numeral systems, I suggest you read about them. Here is how to convert a binary value to a decimal:

  1. Start from the last(right-most) digit. It has a “weight” of (2 to the power of 0) = 1.
  2. If that digit is 1, add 1 to the result.
  3. Go one digit to the left. It has twice the weight of the previous digit. If that digit is 1, add its weight to the result.
  4. Repeat step 3 until you pass through all positions.

Here is an example:

01012 = ? (dec)

    Step 1: The right-most digit has a weight of 1.
    Step 2: Its value is 1, so we begin to sum the result: result = 1
    Step 3: The next digit to the left has twice the previous weight 1 * 2= 2. The digit in that position is 0, so we don't sum, but skip to the next digit to the left.
    Repeat 3: This position has a weight of 2 * 2 = 4. The current digit is 1, so we add 4 to the result. result = 1 + 4.
    Repeat 3: The left-most position has a weight of 4 * 2 = 8. Its digit is 0, so we don't sum and since that was the last digit, we are done.

    Result = 1 + 4 = 5

    01012 = 510

One more example:

    101012 = ?(dec)

    weight 1 * 1 = 1
    weight 2 * 0 = 0
    weight 4 * 1 = 4
    weight 8 * 0 = 0
    weight 16 * 1=16
    result = 1 + 4 + 16 = 21

    101012 = 2110

Try to convert several binary numbers with 2 to 5 digits and then test your result with the tool above.

Binary to decimal converter – C implementation

    You can download the source code of the example implementation from here: bin-to-dec.zip
    The code is also available on github.

We begin by taking a binary number from the user. Since we don't have a binary data type, we use a character array to keep its value. We create the array with the size of 65. That's because we target at most 64 bit numbers + one terminating symbol at the end. Then we scan for at most 64 symbols and determine the length of the user input.

Normally, at this point, you want to perform validation, to make sure the input contains only zeros and ones. Now we will omit it, so we can focus on the algorithm itself.

To do the conversion we need several variables:

  • decimal will keep the result
  • weight will tell us the weight of the current position
  • i just helps us with the loop. Its meaning is the current position in the binary string.

As described above, first we move to the right most digit. We do this by moving the pointer:

    binary += length – 1;

Then, we organize the loop, which will go through all the digits. Inside the loop we check if the current digit has a value of 1 and if it does we add its weight to the result. Then we change the weight for the next position to the left.

That's it! As they say - not a rocket science :)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
unsigned long binaryToDecimal(char *binary, int length)
{
    int i;
    unsigned long decimal = 0;
    unsigned long weight = 1;
    binary += length - 1;
    weight = 1;
    for(i = 0; i < length; ++i, --binary)
    {
        if(*binary == '1')
            decimal += weight;
        weight *= 2;
    }
    return decimal;
}

int main(void)
{
    char binary[65];
    int length;
    unsigned long decimal;
    scanf("%64s", binary);
    length = strlen(binary);
    decimal = binaryToDecimal(binary, length);
    printf("%s in binary is %lu in decimal\n", binary, decimal);
    return 0;
}

The output of this test program should look like this:

Once you learn how to convert binary to decimal, you can learn how to do the opposite: decimal to binary conversion.

   Search this site: