In this article, we are going to learn how to convert an Octal number to an equivalent Hexadecimal number. Here we are going to learn about two number systems one is octal and another is Hexadecimal. Octal numbers are base 8 numbers. Hexadecimal numbers are base 16 numbers and represented in numbers from 0 to 9 and A to F.
The binary to Hexadecimal equivalent number table is given below:
Decimal Number | Binary Number | Hexadecimal Number |
0 | 0000 | 0 |
1 | 0001 | 1 |
2 | 0010 | 2 |
3 | 0011 | 3 |
4 | 0100 | 4 |
5 | 0101 | 5 |
6 | 0110 | 6 |
7 | 0111 | 7 |
8 | 1000 | 8 |
9 | 1001 | 9 |
10 | 1010 | A |
11 | 1011 | B |
12 | 1100 | C |
13 | 1101 | D |
14 | 1110 | E |
15 | 1111 | F |
C program to convert Octal to Hexadecimal number
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.
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 <stdio.h>
#include<string.h>
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;
printf("Please enter an octal number: ");
scanf("%d",&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;
}
printf("The binary equivalent of %d is : %ld",tempOctNo,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 <stdio.h>
#include<string.h>
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;
printf("Please enter an octal number: ");
scanf("%d",&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;
}
printf("The binary equivalent of %d is : %ld",tempOctNo,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';
printf("\nThe hexadecimal equivalent of Octal number %d is :",tempOctNo);
for(int i=0; hex[i]!='\0';i++)
{
printf("%c",hex[i]);
}
return 0;
}
';
printf("\nThe hexadecimal equivalent of Octal number %d is :",tempOctNo);
for(int i=0; hex[i]!='
#include <stdio.h>
#include<string.h>
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;
printf("Please enter an octal number: ");
scanf("%d",&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;
}
printf("The binary equivalent of %d is : %ld",tempOctNo,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';
printf("\nThe hexadecimal equivalent of Octal number %d is :",tempOctNo);
for(int i=0; hex[i]!='\0';i++)
{
printf("%c",hex[i]);
}
return 0;
}
';i++)
{
printf("%c",hex[i]);
}
return 0;
}
Output
Please enter an octal number: 123
The binary equivalent of 123 is : 1010011
The hexadecimal equivalent of Octal number 123 is :53