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.
Input the binary value | The decimal number is | |
---|---|---|
|
|
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:
Here is an example:
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 = 510One 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
Try to convert several binary numbers with 2 to 5 digits and then test your result with the tool above.
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:
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.