Program for conversion of a number from one form to other using C++

Rahul Sah Mar 05, 2021

Hello learners, in this C++ article we are going to learn how we can convert a decimal number to binary i.e, number with base 2 and vice versa. Similarly for Octal and Hexadecimal numbers.

In this article, we are going to learn conversion one number into another form. We will be looking for code and approaches for conversions using a C++ programming language.

Let's learn what basically Decimal, Binary, Octal, and Hexadecimal number means.

Any number which can be represented using 10 digits i.e, from 0-9 is called a decimal number.

Any number which can be represented using 2 digits i.e, 0 and 1 is called a binary number.

Any number which can be represented using 8 digits i.e, from 0-7 is called an octal number.

Any number which can be represented using 10 digits i.e, from 0-9  and 6 Characters i.e, A-F is called a Hexadecimal number.

Table :

Hex Decimal Octal Binary
0 0 0 0
1 1 1 1
2 2 2 10
3 3 3 11
4 4 4 100
5 5 5 101
6 6 6 110
7 7 7 111
8 8 10 1000
9 9 11 1001
A 10 12 1010
B 11 13 1011
C 12 14 1100
D 13 15 1101
E 14 16 1110
F 15 17 1111
10 16 20 10000
20 32 40 100000
40 64 100 1000000
80 128 200 10000000
100 256 400 100000000
200 512 1000 1000000000
400 1024 2000 10000000000

Steps for the conversion of decimal number to their binary form:

First, divide the decimal number by 2 till the quotient is zero and get the remainder for each division. Write the sequence of reminder from bottom to top fashion.

Example :

Convert 1310 to binary number.

Division
by 2
Quotient Remainder Bit #
13/2 6 1 0
6/2 3 0 1
3/2 1 1 2
1/2 0 1 3

So 1310 = 11012

Code :

void decToBinary(long long int n) 
{  
    int binaryNum[32]; 
    int i = 0; 
    while (n > 0) { 
        binaryNum[i] = n % 2; 
        n = n / 2; 
        i++; 
    } 
    for (int j = i - 1; j >= 0; j--) 
        cout << binaryNum[j]; 
} 

Steps for the conversion of decimal number to their Octal form:

First, divide the decimal number by 8 till the quotient is zero and get the remainder for each division. Write the sequence of reminder from bottom to top fashion.

Example :

Convert (57)10 to octal number.

Division
by 8
Quotient

Remainder

(Digit)
Digit #
(57)/8 7 1 0
(7)/8 0 7 1


So (57)10= (71)8

Code :

void decToOctal(long long int n)
{
    int octalNum[100];
    int i = 0;
    while (n != 0) {
        octalNum[i] = n % 8;
        n = n / 8;
        i++;
    }
    for (int j = i - 1; j >= 0; j--)
        cout << octalNum[j];
}

 

Steps for the conversion of decimal number to their Hexadecimal form:

First, divide the decimal number by 16 till the quotient is zero and get the remainder for each division to replace the decimals in hexadecimal form from the table. Write the sequence of reminder from bottom to top fashion.

Example:

Convert 756210 to hex:

Division
by 16
Quotient
(integer)
Remainder
(decimal)
Remainder
(hex)
Digit #
7562/16 472 10 A 0
472/16 29 8 8 1
29/16 1 13 D 2
1/16 0 1 1 3

So 756210 = 1D8A16

Code:

void decToHexa(long long int n) 
{
    char hexaDeciNum[100];
    int i = 0; 
    while(n != 0) 
    {    
        int temp  = 0; 
        temp = n % 16; 
        if (temp < 10) 
        { 
            hexaDeciNum[i] = temp + 48-32; 
            i++; 
        } 
        else
        { 
            hexaDeciNum[i] = temp + 87-32; 
            i++; 
        } 
        n = n / 16; 
    } 
    for(int j = i - 1; j >= 0; j--) 
    {
        cout<<hexaDeciNum[j]; 
    }
} 

Steps for the conversion of Binary number to decimal form:

For binary number with n digits:

dn-1 ... d3 d2 d1 d0

The decimal number is equal to the sum of binary digits (dn) times their power of 2 (2n):

decimal = d0×20 + d1×21 + d2×22 + ... so on

Example

Find the decimal value of 1110012:

binary number: 1 1 1 0 0 1
power of 2: 25 24 23 22 21 20

So,1110012 = 1⋅25+1⋅24+1⋅23+0⋅22+0⋅21+1⋅20 = 5710

Code :

long long  binaryToDecimal(long long int n)
{
    long long int num = n;
    long long int dec_value = 0;
    long long int base = 1;
    long long int temp = num;
    while (temp) {
        int last_digit = temp % 10;
        temp = temp / 10;
 
        dec_value += last_digit * base;
 
        base = base * 2;
    }
    return dec_value;
}

Steps for the conversion of Binary number to their hexadecimal form:

Consider a binary number, group them in a group of 4 digits from the end and write the hexadecimal equivalent of that number from the table given above.

Example 

Convert (01001110)2 to hex:

(0100)2 = (4)16

(1110)2 = (E)16

So

(01001110)2 = (4E)16

Code:

In this code, I have just converted binary to decimal form and then decimal to hexadecimal but one can code using the above logic.

 

Steps for the conversion of Binary number to their Octal form:

Consider a binary number, group them in a group of 3 digits from the end and write the hexadecimal equivalent of that number from the table given above.

Binary
Number
Octal
Number
0 0
1 1
10 2
11 3
100 4
101 5
110 6
111 7
1000 10
1001 11
1010 12
1011 13
1100 14
1101 15
1110 16
1111 17
10000 20
10001 21
10010 22
10011 23
10100 24
10101 25
10110 26
10111 27
11000 30
11001 31
11010 32
11011 33
11100 34
11101 35
11110 36
11111 37
100000 40
1000000 100
10000000 200
100000000 400

Example :

Convert binary number 1101100010102 into an octal number.

1101100010102 = 110 110 001 010 = 110(=6) 110(=6) 001(=1) 010(=2) = 66128

Code :

long long int bin_to_oct(long long bin)
{
    long long int octal, place;
    int i = 0, rem, val;
    octal = 0ll;
    place = 0ll;
    place = 1;
    while (bin > 0) {
        rem = bin % 1000;
 
        switch (rem) {
        case 0:
            val = 0;
            break;
        case 1:
            val = 1;
            break;
        case 10:
            val = 2;
            break;
        case 11:
            val = 3;
            break;
        case 100:
            val = 4;
            break;
        case 101:
            val = 5;
            break;
        case 110:
            val = 6;
            break;
        case 111:
            val = 7;
            break;
        }
 
        octal = (val * place) + octal;
        bin /= 1000;
 
        place *= 10;
    }
 
    return octal;
}

 

Steps for the conversion of Hexadecimal number to their Decimal form:

Convert a Hexadecimal number to their decimal form:

A regular decimal number is the sum of the digits multiplied with a power of 10.

137 in base 10 is equal to each digit multiplied with its corresponding power of 10:

13710 = 1×102+3×101+7×100 = 100+30+7

Hex numbers are read the same way, but each digit counts the power of 16 instead of the power of 10.

For hex number with n digits:

dn-1 ... d3 d2 d1 d0

Multiply each digit of the hex number with its corresponding power of 16 and sum:

decimal = dn-1×16n-1 + ... + d3×163 + d2×162 + d1×161+d0×160

Example:

3B in base 16 is equal to each digit multiplied with its corresponding 16n:

3B16 = 3×161+11×160 = 48+11 = 5910

Code:

In this code, we have used the shortcut technique firstly we have converted hexadecimal numbers to binary numbers using the reverse of grouping technique and then binary to decimal number.

Steps for the conversion of Hexadecimal number to their binary form:

Convert every hex digit (start the lowest digit) to 4 binary digits, with the above table.

Example:

So 4CD16 = (0100)(1100)(1101)2

Code:

long long int hex_to_bin(string hex)
{
    long long int bin, place;
    int i = 0, rem, val;
    bin = 0ll;
    place = 0ll;
    for (i = 0; i<hex.size(); i++) {
        bin = bin * place;
        switch (hex[i]) {
        case '0':
            bin += 0;
            break;
        case '1':
            bin += 1;
            break;
        case '2':
            bin += 10;
            break;
        case '3':
            bin += 11;
            break;
        case '4':
            bin += 100;
            break;
        case '5':
            bin += 101;
            break;
        case '6':
            bin += 110;
            break;
        case '7':
            bin += 111;
            break;
        case '8':
            bin += 1000;
            break;
        case '9':
            bin += 1001;
            break;
        case 'a':
        case 'A':
            bin += 1010;
            break;
        case 'b':
        case 'B':
            bin += 1011;
            break;
        case 'c':
        case 'C':
            bin += 1100;
            break;
        case 'd':
        case 'D':
            bin += 1101;
            break;
        case 'e':
        case 'E':
            bin += 1110;
            break;
        case 'f':
        case 'F':
            bin += 1111;
            break;
        default:
            cout << "Invalid hexadecimal input.";
        }
 
        place = 10000;
    }
 
    return bin;
}

Steps for the conversion of Hexadecimal number to their Octal form:

First, convert Hexadecimal number to binary number using the reverse of grouping technique and then convert binary number to octal number by grouping in a group of 3.

Example

So, the binary of (1A6)16 = 0001 1010 0110

Now after finding the binary of the hexadecimal number now the next task is to find the octal of the binary number.

Before that, we will group the binary number into three. After grouping in 3, we will get 000 110 100 110

So the octal representation of a hexadecimal number (1A6)16 is (646)8.

Code:

long long int hex_to_oct(string hex)
{
    long long int octal, bin;
    bin = hex_to_bin(hex);
    octal = bin_to_oct(bin);
 
    return octal;
}

Steps for the conversion of Octal number to their Decimal form:

Let see how we can convert an octal number to a decimal number. For that, we just start with the digit at the unit's place and keep going from right to left. Multiply every digit with raised power of 8 to the position of the digit (starting from zero) from right to left and sum up. The resulting number so formed is in Decimal representation.

Example:

So (345)8= (3 * 82) + (4 * 81) + (5 * 80) = (3 * 64) + (4 * 8) + (5 * 1) = (229)10

Code:

long long int octalToDecimal(long long int n) 
{ 
    int num = n; 
    int dec_value = 0; 
    int base = 1; 
    int temp = num; 
    while (temp) { 
        int last_digit = temp % 10; 
        temp = temp / 10;
        dec_value += last_digit * base; 
  
        base = base * 8; 
    } 
    return dec_value; 
} 

 

Steps for the conversion of Octal number to their Binary form:

We will see the steps for the conversion of Octal number to binary number. We first write each octal digit to its 3-digit binary representation. Each of the digits must be treated as a decimal value. Then combine these binary representations to form a single binary number.

In the code, first, the number is converted to a decimal number and then the decimal number is converted to a binary number.

Example:

 Octal Number -> 3     7     5
                                 ↓     ↓    ↓     
Binary Number -> 011 111 101
 
So (375)8=(011111101)2    
 

Steps for the conversion of Octal number to their Hexadecimal form:

There are various approaches for the conversion of octal numbers to hexadecimal form. One approach is shown stepwise below

Let's see one approach, get an octal number from the user. Convert the provided octal number to binary then group the binary bit in the group of 4, starting from the right side, and write the corresponding hexadecimal of extracted 4 binary bits.

In the code, first, the number is converted to a decimal number and then the decimal number is converted to a hexadecimal number.

Example:

Convert 14228 into Hexadecimal number.

First ,convert 14228 into decimal, by using above steps:

= 14228
1 × 834 × 822 × 812 × 80
= 78610

Now, we have to convert 78610 to hexadecimal

786 / 16 = 49 with remainder 2
49 / 16 = 3 with remainder 1
3 / 16 = 0 with remainder 3

Then just write down the remainders in the reverse order to get the answer, The octal number 1422 converted to hexadecimal is therefore equal to 312

 

In this way, you can convert a number from one form to another using the C++ language. I hope it was easy enough to understand. If you have any doubt, feel free to ask in the comment section. All the best!

Thank you!

Regards,
Rahul Sah
Codespeedy Tech Pvt. Ltd.

Project Files

Loading...
..
This directory is empty.

Comments (0)

Leave a Comment