# C++ program to convert Octal to Hexadecimal

In this article, we are going to learn how to convert a Octal number to an equivalent Hexadecimal number in C++. Here we are going to learn about two number systems one is octal and another is Hexadecimal.
Octal numbers are based on 8 numbers. Hexadecimal numbers are base 16 numbers and represented in numbers from 0 to 9 and A to F.To convert an octal number to hexadecimal, there is no direct path. It first needs to be converted to binary and then binary to hexadecimal. The binary to Hexadecimal equivalent number table is given below:

### C++ program to convert Octal to Hexadecimal

In this program example, we are going to learn how we will convert an octal number to binary first and then binary to hexadecimal. This will give us the hexadecimal number equivalent to the octal number.

```#include<iostream>
#include<string.h>
using namespace std;

int main()
{
int BINCONSTANTS[]={0,1,10,11,100,101,110,111};
char hexNum[10];
char hex[10];
long int binNum=0;
int octalNum,tempOctNo;
int rem=0;
int position=1;
int len=0;
int k=0;

cout <<"Please enter an octal number: ";
cin >> octalNum;

tempOctNo = octalNum;

// First Convert the octal number to binary quivalent
while(octalNum != 0)
{
rem= octalNum % 10;
binNum= BINCONSTANTS[rem]*position+binNum;
octalNum= octalNum / 10;
position=position*1000;
}

cout <<"The binary equivalent of "<< tempOctNo << " is " << binNum;

// From the binary equivalent now create the hexadecimal number
while(binNum > 0)
{
rem = binNum % 10000;
switch(rem)
{
case 0:
strcat(hexNum, "0");
break;
case 1:
strcat(hexNum, "1");
break;
case 10:
strcat(hexNum, "2");
break;
case 11:
strcat(hexNum, "3");
break;
case 100:
strcat(hexNum, "4");
break;
case 101:
strcat(hexNum, "5");
break;
case 110:
strcat(hexNum, "6");
break;
case 111:
strcat(hexNum, "7");
break;
case 1000:
strcat(hexNum, "8");
break;
case 1001:
strcat(hexNum, "9");
break;
case 1010:
strcat(hexNum, "A");
break;
case 1011:
strcat(hexNum, "B");
break;
case 1100:
strcat(hexNum, "C");
break;
case 1101:
strcat(hexNum, "D");
break;
case 1110:
strcat(hexNum, "E");
break;
case 1111:
strcat(hexNum, "F");
break;
}

len=len+1;
binNum /= 10000;
}
for(int i=len-1;i>=0;i--)
{
hex[k]=hexNum[i];
k++;
}
hex[len]='#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int BINCONSTANTS[]={0,1,10,11,100,101,110,111};
char hexNum[10];
char hex[10];
long int binNum=0;
int octalNum,tempOctNo;
int rem=0;
int position=1;
int len=0;
int k=0;
cout <<"Please enter an octal number: ";
cin >> octalNum;
tempOctNo = octalNum;
// First Convert the octal number to binary quivalent
while(octalNum != 0)
{
rem= octalNum % 10;
binNum= BINCONSTANTS[rem]*position+binNum;
octalNum= octalNum / 10;
position=position*1000;
}
cout <<"The binary equivalent of "<< tempOctNo << " is " << binNum;
// From the binary equivalent now create the hexadecimal number
while(binNum > 0)
{
rem = binNum % 10000;
switch(rem)
{
case 0:
strcat(hexNum, "0");
break;
case 1:
strcat(hexNum, "1");
break;
case 10:
strcat(hexNum, "2");
break;
case 11:
strcat(hexNum, "3");
break;
case 100:
strcat(hexNum, "4");
break;
case 101:
strcat(hexNum, "5");
break;
case 110:
strcat(hexNum, "6");
break;
case 111:
strcat(hexNum, "7");
break;
case 1000:
strcat(hexNum, "8");
break;
case 1001:
strcat(hexNum, "9");
break;
case 1010:
strcat(hexNum, "A");
break;
case 1011:
strcat(hexNum, "B");
break;
case 1100:
strcat(hexNum, "C");
break;
case 1101:
strcat(hexNum, "D");
break;
case 1110:
strcat(hexNum, "E");
break;
case 1111:
strcat(hexNum, "F");
break;
}
len=len+1;
binNum /= 10000;
}
for(int i=len-1;i>=0;i--)
{
hex[k]=hexNum[i];
k++;
}
hex[len]='\0';
cout <<"\nThe hexadecimal equivalent of Octal number " << tempOctNo << " is : ";
for(int i=0; hex[i]!='\0';i++)
{
cout << hex[i];
}
return 0;
}
';

cout <<"\nThe hexadecimal equivalent of Octal number " << tempOctNo << " is : ";
for(int i=0; hex[i]!='#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int BINCONSTANTS[]={0,1,10,11,100,101,110,111};
char hexNum[10];
char hex[10];
long int binNum=0;
int octalNum,tempOctNo;
int rem=0;
int position=1;
int len=0;
int k=0;
cout <<"Please enter an octal number: ";
cin >> octalNum;
tempOctNo = octalNum;
// First Convert the octal number to binary quivalent
while(octalNum != 0)
{
rem= octalNum % 10;
binNum= BINCONSTANTS[rem]*position+binNum;
octalNum= octalNum / 10;
position=position*1000;
}
cout <<"The binary equivalent of "<< tempOctNo << " is " << binNum;
// From the binary equivalent now create the hexadecimal number
while(binNum > 0)
{
rem = binNum % 10000;
switch(rem)
{
case 0:
strcat(hexNum, "0");
break;
case 1:
strcat(hexNum, "1");
break;
case 10:
strcat(hexNum, "2");
break;
case 11:
strcat(hexNum, "3");
break;
case 100:
strcat(hexNum, "4");
break;
case 101:
strcat(hexNum, "5");
break;
case 110:
strcat(hexNum, "6");
break;
case 111:
strcat(hexNum, "7");
break;
case 1000:
strcat(hexNum, "8");
break;
case 1001:
strcat(hexNum, "9");
break;
case 1010:
strcat(hexNum, "A");
break;
case 1011:
strcat(hexNum, "B");
break;
case 1100:
strcat(hexNum, "C");
break;
case 1101:
strcat(hexNum, "D");
break;
case 1110:
strcat(hexNum, "E");
break;
case 1111:
strcat(hexNum, "F");
break;
}
len=len+1;
binNum /= 10000;
}
for(int i=len-1;i>=0;i--)
{
hex[k]=hexNum[i];
k++;
}
hex[len]='\0';
cout <<"\nThe hexadecimal equivalent of Octal number " << tempOctNo << " is : ";
for(int i=0; hex[i]!='\0';i++)
{
cout << hex[i];
}
return 0;
}
';i++)
{
cout << hex[i];

}

return 0;
}
```

Output

```Please enter an octal number: 15
The binary equivalent of 15 is 1101
The hexadecimal equivalent of Octal number 15 is : D

```