In this tutorial, we will write a program to convert an octal number into hexadecimal in C++. Before that, you must have knowledge of the following topics in C++.
Hexadecimal number
The hexadecimal number is represented with a base of 16. It has digits from 0 to 15 to represent, However after 9 the values are represented in Alphabet till 15 such as 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.
Octal number
The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.
Let us go through a program for the Octal to Hexadecimal Conversion in C++.
C++ Program to Convert Octal to Hexadecimal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | #include <iostream> #include <string.h> #include <math.h> using namespace std; int main() { char octalNumber[20]; int arr1[20], arr2[20], rev[20]; int h, i, j, k, l, x, fr, flag, rem, n1, n3; float rem1, n2, n4, dno; x = fr = flag = rem = 0; rem1 = 0.0; cout << "Enter an Octal number: "; cin >> octalNumber; for (i = 0, j = 0, k = 0; i < strlen(octalNumber); i++) { if (octalNumber[i] == '.') flag = 1; else if (flag == 0) arr1[j++] = octalNumber[i] - 48; else if (flag == 1) arr2[k++] = octalNumber[i] - 48; } x = j; fr = k; for (j = 0, i = x - 1; j < x; j++, i--) { rem = rem + (arr1[j] *pow(8, i)); } for (k = 0, i = 1; k < fr; k++, i++) { rem1 = rem1 + (arr2[k] / pow(8, i)); } rem1 = rem + rem1; dno = rem1; n1 = (int) dno; n2 = dno - n1; i = 0; while (n1 != 0) { rem = n1 % 16; rev[i] = rem; n1 = n1 / 16; i++; } j = 0; while (n2 != 0.0) { n2 = n2 * 16; n3 = (int) n2; n4 = n2 - n3; n2 = n4; arr1[j] = n3; j++; if (j == 4) break; } l = i; cout << "Equivalent hexadecimal value: "; for (i = l - 1; i >= 0; i--) { if (rev[i] == 10) cout << "A"; else if (rev[i] == 11) cout << "B"; else if (rev[i] == 12) cout << "C"; else if (rev[i] == 13) cout << "D"; else if (rev[i] == 14) cout << "E"; else if (rev[i] == 15) cout << "F"; else cout << rev[i]; } } |
Output:
Enter an Octal number: 377
Equivalent hexadecimal value: FF
The above program calculates the natural part of the octal number. If you also want the program the decimal octal number to decimal hexadecimal number then add the following code after the end of the last for loop on the above program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | h = j; cout << "."; for (k = 0; k < h; k++) { if (arr1[k] == 10) cout << "A"; else if (arr1[k] == 11) cout << "B"; else if (arr1[k] == 12) cout << "C"; else if (arr1[k] == 13) cout << "D"; else if (arr1[k] == 14) cout << "E"; else if (arr1[k] == 15) cout << "F"; else cout << arr1[k]; } |
Then you will get output something like this,
Enter an Octal number: 137.6
Equivalent hexadecimal value: 5F.C
You may go through the vice-versa program: