Help anyone?

Write a program that reads a 4-digit number into a c-string such as binary 1101 (or decimal 0013), and an integer indicating the numeric base of the number, such as 2 for binary (or 10 for decimal) and assigns the decimal value of the c-string, such as 13 for 1101 (or 13 for 0013) to an integer variable and prints that variable.

The program must first convert each bit from its ASCII value (such as 48 for 0 or 49 for 1, etc.) to its numeric value, such as 0, 1, etc. The ASCII value of 0, for example, is 48 and for 1 it's 49 which obviously are not the same as their numeric values. It must then multiply each digit by its corresponding weight. The least significant bit (rightmost bit) has a weight of 1 and as you go from right to left - least to most significant bit - the weight of each digit is multiplied by the base (1, 2, 4, 8 for binary and 1, 10, 100, 1000 for decimal, etc.). Add all above products to get the decimal value and assign to the integer variable. For example, binary 1101 should give you 13 (8 + 4 + 0 + 1), 0795 in decimal will be 795 (0*1000+7*100+9*10+5*1) , etc.

print the output value like so:

1101 in base 2 = 13, or

0795 in base 10 = 795.

Remember, you're reading the input into a c-string and converting it to an integer before printing the result.


Last edited on
What help? Your homework. Your opportunity to learn by doing.

You did not show the code that you have written so far. You did not ask any question about the problems that you have in your code.
I don't understand how to do it.
Not any part of it? If so, where/why did you get this task (from)?
Topic archived. No new replies allowed.