People count in tens, probably because they have ten fingers. Computers work in twos. Programmers also meet base 8 and base 16. Moving a number from one base to another is a routine task that rests on two short algorithms.
Positional Number Systems
Definition: Base. The number of distinct digit symbols used in a positional number system; it is also the factor by which the place value grows from one position to the next.
In base b, the allowed digits are 0 to b − 1, and the place values, from the right, are 1, b, b², b³ and so on. Table 3.6 lists the common bases.
Table 3.6: Common number bases
| Base | Name | Digits used | The value thirteen |
|---|---|---|---|
| 2 | Binary | 0, 1 | 1101 |
| 8 | Octal | 0 to 7 | 15 |
| 10 | Decimal | 0 to 9 | 13 |
| 16 | Hexadecimal | 0 to 9, A to F | D |
Validating the Digits
Before a number is converted, each of its digits must be checked against the stated base. Every digit must be less than the base. The number 1021 cannot be a binary number, because the digit 2 is not allowed in base 2. Likewise, 289 is not a valid octal number. A program should reject such input instead of producing a meaningless answer. The base itself must also lie in the supported range.
From Any Base to Decimal
The value is built from left to right. Start with value = 0. For each digit, multiply the value so far by the base and add the digit:
value = value * base + digit
For the binary number 1011: 0 × 2 + 1 = 1, then 1 × 2 + 0 = 2, then 2 × 2 + 1 = 5, then 5 × 2 + 1 = 11. So 1011 in base 2 equals 11 in base 10. The same answer follows from the place values: 1 × 8 + 0 × 4 + 1 × 2 + 1 × 1 = 11.
When the number is held as an integer, its digits come off from the right. In that case each digit is multiplied by its place value, which starts at 1 and is multiplied by the base after every digit.
From Decimal to Any Base
The method is repeated division by the new base. The remainders, read in reverse order, are the digits of the answer.
The remainders appear in the order 1, 1, 0, 1, which is right to left. To display them in the correct order, they are stored and then displayed in reverse, or the result is assembled as a decimal-looking integer by multiplying each remainder by a place value of 1, 10, 100 and so on.
Converting Between Two Bases
To convert between two bases neither of which is 10, the number is first converted to decimal and then from decimal to the target base. This two-stage approach is another example of decomposition.
Example 3.6: Base conversion with validation
Problem: Read a number and its base (2 to 10). Validate the digits, convert the number to decimal, and then display it in binary.
Solution:
- Extract the digits from the right. Reject the number if any digit is not less than the base.
- Multiply each digit by its place value and add it to value.
- Convert value to binary by repeated division by 2, assembling the digits with place values 1, 10, 100.
#include <stdio.h>
int main(void)
{
int num, base, temp, digit, value = 0, place = 1;
int valid = 1, binary = 0, bplace = 1;
printf("Enter number and its base (2-10): ");
scanf("%d %d", &num, &base);
for (temp = num; temp > 0; temp = temp / 10)
{
digit = temp % 10;
if (digit >= base)
{
valid = 0;
}
value = value + digit * place;
place = place * base;
}
if (base < 2 || base > 10 || valid == 0)
{
printf("\nInvalid digit or base\n");
return 0;
}
printf("\nDecimal value = %d\n", value);
for (temp = value; temp > 0; temp = temp / 2)
{
binary = binary + (temp % 2) * bplace;
bplace = bplace * 10;
}
printf("Binary value = %d\n", binary);
return 0;
}
Input:
157 8
Output:
Enter number and its base (2-10):
Decimal value = 111
Binary value = 1101111
Result: All digits of 157 are less than 8, so it is a valid octal number. Its decimal value is 1 × 64 + 5 × 8 + 7 = 111, and 111 in binary is 1101111.
Common mistakes
- Converting a number without checking that every digit is allowed in its base.
- Reading the remainders of repeated division in the order they were produced instead of in reverse.
- Multiplying by 10 instead of by the base when accumulating the value.
Key takeaways
- In base b the digits are 0 to b − 1 and the place values are powers of b.
- Every digit must be validated against the input base before conversion.
- To decimal: accumulate with
value = value * base + digit. - From decimal: divide repeatedly by the new base and read the remainders in reverse.
Practice questions
- Convert the binary number 1101 to decimal.
- Convert the decimal number 25 to binary.
- Is 482 a valid octal number? Give a reason.
Answers to the practice questions
- 1 × 8 + 1 × 4 + 0 × 2 + 1 × 1 = 13.
-
- No. The digit 8 is not allowed in base 8, whose digits are 0 to 7.
Sources
- Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit III
