# C Programming – Binary to Decimal Converter There are 10 kinds of people in this world, those who understand binary and those who don’t. — Unknown

While reading a book on assembly language programming, I came across a section that discussed different numbering systems used and how to convert from one to another. I gave myself some assignments to complete one at a time. Here’s the first one. This converter takes an 8-bit, unsigned binary number, entered at the command line as argv, and converts it to its decimal equivalent.

This was a fun assignment. It can easily be modified to take a 16-bit number instead, simply by changing the number of iterations in the for() loop and the starting number for the myCount variable. It uses the pow() function in math.h but everything else in the code is pretty straight forward. I have not tested it fully but it does compile and work using the Visual Studio 2012 command line compiler.

```// function receives a string containing an 8-bit, unsigned binary integer
// returns the decimal integer value of the binary integer
#include <stdio.h>
#include <string.h>
#include <math.h>
int main(int argc, char *argv[])
{
const char *binNumber = argv;
int decNumber = 0;
int myDecCount;
int myCount = 7;

puts("\n\nBinary to Decimal Converter\nby David Peterson Harvey\n\n");

if (argc == 2)
{
printf("%s converts to %d\n\n", binNumber, decNumber);
// if argv == 8 digits long, binNumber = {argv};
if(strlen(argv) == 8)
{
for(myDecCount=0; myDecCount <=7; myDecCount++)
{
if(binNumber[myCount] == '1')
{
decNumber = decNumber + pow(2, myDecCount);
}
printf("\nCurrent number %d.\nCurrent count %d.\n\n", myDecCount, decNumber);
myCount--;
}
}
else
{
printf("You must enter a binary number 8 digits long. You entered %s.\n\n", argv);
}
}
else
{
printf("To use program:\n\n\t %s 8-digit_binary_number\n\n", argv);
}
return 0;
}```