Number Systems And Their Conversions Using C++

Number systems are the techniques to represent numbers in the computer system.

Rajendra Prajapat
Dev Genius

--

As a programmer, It is essential to have a basic knowledge of number systems. We have four major number systems:

  1. Decimals
  2. Binary
  3. Octal
  4. Hexadecimal

1. Decimals

The most commonly used number system in our day-to-day life is decimals. This number system has base-10, and all numbers are created with ten digits: 0,1,2,3, 4,5,6,7,8, 9. In the decimal number system, each position represents a specific power of 10. Here is an example:

1562 = 1 * 10³ + 5 * 10² +6 *10¹ +2*10⁰
= 1*1000 + 5*100 + 6*10 + 2
one thousand five hundred sixty two

2. Binary

It is a base-2 number system that uses two digits 0 and 1. In computer science, a single digit is referred to as a bit, which is the smallest unit of memory in computers. Each position, in a binary number, represents a specific power of two. Let’s see an example:

1010 = 1*2³ + 0*2² + 1*2¹ + 0*2⁰
= 1*8 + 0*4 + 1*2 + 0*1
= 8 + 0 + 2 + 0
= 10
// That means 1010 in binary is equvilent to 10 in decimal number system.

3. Octal

It is a base-8 number system that uses eight digits 0,1,2,3,4,5,6,7. It is also known as Oct. Position of each digit, in an octal number, has a weight, which equals to a specific power of 8. Here is an example:

25624 = 5*8⁴ + 5*8³ + 6*8² +2*8¹ + 4*8⁰
= 5*4096 + 5*512 + 6*64 + 2*8 + 4*1
= 23444
// That means 25624 in octal number system is equivalent to 23444 in decimal number system

4. Hexa-Decimal

Hexadecimal number system, also known as hex, is a base-16 number system that uses 10 digits and 6 letters: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F.

HexaDecimal      decimal 
A 10
B 11
C 12
D 13
E 14
F 15

In computer systems, storing 32-bit or 64-bit data is a difficult task. So these 32-bit or 64-bit data is arranged in 4-bit groups and converted to the hexadecimal number system. The position of each digit or letter in hex represents a specific power of 16.

Example: 
8A1F = 8*16³ + 10*16² + 1*16¹ + 15*16⁰
= 8*4096 + 10*256 + 1*16 + 15*1
= 35359
//It implies that 8A1F in hex is equivalent to 35359 in decimal

Conversions

Any base to decimal number system

As we have seen, in all the examples above, that position of each digit or letter represents the specific power of the new base. we will make use of it to convert any base to the decimal number system.

int toDecimal(string number,int base)
{
int decimal= 0;
int size = number.length();
for(int i=0; i<size; i++) //check for each position
{
//calculate digit * base^position for each position

if(number[i]>='A') // if it has more than 10 digits
{
ans += (number[i]-'A'+10) *pow(base,size-i-1);
}
else
{
ans += (number[i]-'0') *pow(base,size-i-1);
}
}
return ans;
}

Decimal to Any Number System

To convert a decimal to another base, we can follow these steps:

  1. Divide the decimal number with a new base and replace the current decimal to the quotient.
  2. Store the remainder to a list.
  3. Go to step 1 if the decimal number is not equal to 0.
  4. Save all the remainders to a string in reverse order.

let’s convert 12(base-10) to binary(base-2)

division         quotient             remainder
12/2 6 0
6/2 3 0
3/2 1 1
1/2 0 1
list of remainders : 0 0 1 1
reverse of list : 1 1 0 0
list to string : 1100(base-2)

In our program, instead of storing the remainder in a list and then reverse it, we will use a stack here to save remainders. In our program “singleDigitPreprocess” function will covert a remainder to a digit or letter based on the new base.

char singleDigitPreprocees(int n)
{
if(n<10) return n+'0';
else if(n >= 10){
return 'A'+(n-10);
}
}
string decimalTo(int decimal,int base = 16)
{
stack<char> remainders;
if(decimal == 0) return "0";
while(decimal > 0)
{
char rem = singleDigitPreprocees(decimal%base);
decimal = decimal/base;
remainders.push(rem);
}
string any_base = "";
while(!remainders.empty())
{
any_base += remainders.top();
remainders.pop();
}
return any_base;
}

If we know how to convert a number base-10 number to any base and any base number to base-10 number, we can convert any base number to any other base number.

Let’s say we want to convert a base-2 number to base-8 number. One way to do so is first, we will convert the base-2 to decimal then decimal to base-8 number. But other ways also there, to convert one number system to another. For example, to convert a number from binary to octal, we can use a simple trick. The trick is to the first group the bits of size three from the right-most position, and then convert these groups to decimal, because numbers from 0 to 7 are the same in both(decimal and octal) number systems. If a group of 3 doesn’t complete, we can add extra 0s to the left-most position.

Lets convert 10101(base-2) to octal
10101 => 010 101
010(base-2) = 2(base-10) = 2(base-8)
101(base-2) = 5(base-10) = 5(base-8)
10101(base-2) => 010 101 => 2 5 => 25(base-8)

The C++ code for all the conversions can be found here.

--

--

SDE@Sharechat | Competitive Programmer | MERN Stack Developer| Interested in Computer Networks, CyberSecurity, and Machine Learning field