# How to convert Hexadecimal to Octal in C++

In this article, we are going to learn how to convert Hexadecimal to Octal in C++. Here we are going to learn about two number systems one is Hexadecimal and another is Octal. Hexadecimal numbers are base 16 numbers and represented in numbers from 0 to 9 and A to F.Octal numbers are base 8 numbers and represented in numbers from 0 to 7.

### How to convert Hexadecimal to Octal

In this example, we are going to convert a hex number to an octal number. But this is a two-step process, so first, we are going to convert the hexadecimal number to a binary number and then the binary number to an octal number.

```#include<iostream>
#include <string.h>

using namespace std;

int main()
{
char hexNum[17];
long long octalNum, binNum, place;
int  rem, val;

cin >> hexNum;

octalNum    = 0ll;
binNum   = 0ll;
place    = 0ll;

for(int i=0; hexNum[i]!='#include<iostream>
#include <string.h>
using namespace std;
int main()
{
char hexNum[17];
long long octalNum, binNum, place;
int  rem, val;
cin >> hexNum;
octalNum    = 0ll;
binNum   = 0ll;
place    = 0ll;
for(int i=0; hexNum[i]!='\0'; i++)
{
binNum = binNum * place;
switch(hexNum[i])
{
case '0':
binNum += 0;
break;
case '1':
binNum += 1;
break;
case '2':
binNum += 10;
break;
case '3':
binNum += 11;
break;
case '4':
binNum += 100;
break;
case '5':
binNum += 101;
break;
case '6':
binNum += 110;
break;
case '7':
binNum += 111;
break;
case '8':
binNum += 1000;
break;
case '9':
binNum += 1001;
break;
case 'a':
case 'A':
binNum += 1010;
break;
case 'b':
case 'B':
binNum += 1011;
break;
case 'c':
case 'C':
binNum += 1100;
break;
case 'd':
case 'D':
binNum += 1101;
break;
case 'e':
case 'E':
binNum += 1110;
break;
case 'f':
case 'F':
binNum += 1111;
break;
default:
cout << "This is not a Valid HexaDecimal Number.";
}
place = 10000;
}
place = 1;
while(binNum > 0)
{
rem = binNum % 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;
}
octalNum = (val * place) + octalNum;
binNum /= 1000;
place *= 10;
}
cout << "\nThe Octal equivalent of HexaDecimal number " <<  hexNum << " is " << octalNum;
return 0;
}
'; i++)
{
binNum = binNum * place;

switch(hexNum[i])
{
case '0':
binNum += 0;
break;
case '1':
binNum += 1;
break;
case '2':
binNum += 10;
break;
case '3':
binNum += 11;
break;
case '4':
binNum += 100;
break;
case '5':
binNum += 101;
break;
case '6':
binNum += 110;
break;
case '7':
binNum += 111;
break;
case '8':
binNum += 1000;
break;
case '9':
binNum += 1001;
break;
case 'a':
case 'A':
binNum += 1010;
break;
case 'b':
case 'B':
binNum += 1011;
break;
case 'c':
case 'C':
binNum += 1100;
break;
case 'd':
case 'D':
binNum += 1101;
break;
case 'e':
case 'E':
binNum += 1110;
break;
case 'f':
case 'F':
binNum += 1111;
break;
default:
cout << "This is not a Valid HexaDecimal Number.";
}

place = 10000;
}

place = 1;

while(binNum > 0)
{
rem = binNum % 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;
}

octalNum = (val * place) + octalNum;
binNum /= 1000;

place *= 10;
}

cout << "\nThe Octal equivalent of HexaDecimal number " <<  hexNum << " is " << octalNum;

return 0;
}
```

Output

```Please enter a Hexadecimal Number: ff

The Octal equivalent of HexaDecimal number ff is 377
```