By Rahul Sah

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.

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 |

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 13_{10} 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 13_{10} = 1101_{2}

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]; }

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.

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}

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]; }

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.

Convert 7562_{10} 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 7562_{10} = 1D8A_{16}

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]; } }

For binary number with n digits:

*d _{n-1} ... d_{3} d_{2} d_{1} d_{0}*

The decimal number is equal to the sum of binary digits (d_{n}) times their power of 2 (2^{n}):

decimal = *d*_{0}×2^{0} + *d*_{1}×2^{1} + *d*_{2}×2^{2} + ... so on

Find the decimal value of 111001_{2}:

binary number: | 1 | 1 | 1 | 0 | 0 | 1 |
---|---|---|---|---|---|---|

power of 2: | 2^{5} |
2^{4} |
2^{3} |
2^{2} |
2^{1} |
2^{0} |

So,111001_{2} = 1⋅2^{5}+1⋅2^{4}+1⋅2^{3}+0⋅2^{2}+0⋅2^{1}+1⋅2^{0} = 57_{10}

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; }

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.

Convert (01001110)_{2} to hex:

(0100)_{2} = (4)_{16}

(1110)_{2} = (E)_{16}

_{So}

(01001110)_{2} = (4E)_{16}

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

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 110110001010_{2} into an octal number.

110110001010_{2} = 110 110 001 010 = 110_{(=6)} 110_{(=6)} 001_{(=1)} 010_{(=2)} = 6612_{8}

**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; }

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:

137_{10} = 1×10^{2}+3×10^{1}+7×10^{0} = 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:

*d _{n-1} ... d_{3} d_{2} d_{1} d_{0}*

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

decimal = *d _{n-1}×16^{n-1} + ... + d_{3}×16^{3} + d_{2}×16^{2} + d_{1}×16^{1}+d_{0}×16^{0}*

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

3B_{16} = 3×16^{1}+11×16^{0} = 48+11 = 59_{10}

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.

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

So 4CD_{16} = (0100)(1100)(1101)_{2}

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; }

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.

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.}

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; }

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.

So (345)_{8}= (3 * 8^{2}) + (4 * 8^{1}) + (5 * 8^{0}) = (3 * 64) + (4 * 8) + (5 * 1) = (229)_{10}

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; }

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.

So (375)_{8}=(011111101)_{2 }

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.

Convert 1422_{8} into Hexadecimal number.

First ,convert 1422_{8} into decimal, by using above steps:

= 1422_{8}

= 1 × 8^{3}4 × 8^{2}2 × 8^{1}2 × 8^{0}

= 786_{10}

Now, we have to convert 786_{10} to hexadecimal

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.

Submitted by Rahul Sah (rahulsah1436)

Download packets of source code on Coders Packet

## Comments