What is a Hexadecimal number system?
A hexadecimal numeral system operates on a base-16, utilizing sixteen symbols: 0-9 and A-F to represent values.
How to convert an octal number into a hexadecimal number
We cannot directly transform an octal numeral system into a hexadecimal numeral system. Initially, we must convert the octal number into a binary numeral system, and subsequently, this binary system will be transformed into a hexadecimal numeral system.
Let's look at the approach.
- Input the octal number.
- An octal number system is converted into a binary number system.
- Extract the 4 bits in a group from the right-side.
- Provide the hexadecimal number to the extracted 4 bits.
For example, the octal number is 123.
Let's develop a basic application that transforms octal numbers into their corresponding hexadecimal equivalents.
Example
#include <stdio.h>
#include<string.h>
int main()
{
int octaltobinary[]={0,1,10,11,100,101,110,111};
char hexadecimal[10];
char hex[10];
long int binary=0;
int octal;
int rem=0;
int position=1;
int len=0;
int k=0;
printf("Enter a octal number");
scanf("%d",&octal);
// Converting octal number into a binary number.
while(octal!=0)
{
rem=octal%10;
binary=octaltobinary[rem]*position+binary;
octal=octal/10;
position=position*1000;
}
printf("The binary number is : %ld",binary);
// Converting binary number into a hexadecimal number.
while(binary > 0)
{
rem = binary % 10000;
switch(rem)
{
case 0:
strcat(hexadecimal, "0");
break;
case 1:
strcat(hexadecimal, "1");
break;
case 10:
strcat(hexadecimal, "2");
break;
case 11:
strcat(hexadecimal, "3");
break;
case 100:
strcat(hexadecimal, "4");
break;
case 101:
strcat(hexadecimal, "5");
break;
case 110:
strcat(hexadecimal, "6");
break;
case 111:
strcat(hexadecimal, "7");
break;
case 1000:
strcat(hexadecimal, "8");
break;
case 1001:
strcat(hexadecimal, "9");
break;
case 1010:
strcat(hexadecimal, "A");
break;
case 1011:
strcat(hexadecimal, "B");
break;
case 1100:
strcat(hexadecimal, "C");
break;
case 1101:
strcat(hexadecimal, "D");
break;
case 1110:
strcat(hexadecimal, "E");
break;
case 1111:
strcat(hexadecimal, "F");
break;
}
len=len+1;
binary /= 10000;
}
for(int i=len-1;i>=0;i--)
{
hex[k]=hexadecimal[i];
k++;
}
hex[len]='\0';
printf("\nThe hexadecimal number is :");
for(int i=0; hex[i]!='\0';i++)
{
printf("%c",hex[i]);
}
return 0;
}
Output
Output
Enter a octal numberThe binary number is : %ld
The hexadecimal number is :%c
Analysis of the above program
- First, we take the user input, which would be an octal number, and it gets stored in an ' octal ' variable.
- After entering the user input, we convert the octal number into binary number. We iterate through the while(octal!=0) loop, which executes till the value of octal is not equal to zero. The final value of a binary number will be stored in a 'binary' variable.
- After calculating the binary number, we will calculate the hexadecimal number. We will iterate through the elements of a binary number by using a while(binary>0) On each iteration, four digits from the right-side of a binary number are extracted and replaced with their corresponding hexadecimal number.
- The hexadecimal variable contains the reverse value of the actual hexadecimal number. In order to get the actual hexadecimal number of an octal number, we define a new variable, i.e., hex . We will iterate the elements of a hexadecimal variable from the last, and stores each element in hex .