Search

# Octal to Hexadecimal in C || C Programs

What is an octal number system?

An octal number system is a number system which is having base-8 where base-8 represents the eight digits from 0 to 7.

What is a Hexadecimal number system?

A hexadecimal number system is a number system which is having base-16 where base-16 represents the sixteen digits, i.e.,0,1,2,3,4,5,6,7,8,9, A, B, C, D, E, F.

How to convert an octal number into a hexadecimal number

We cannot directly convert an octal number system into a hexadecimal number system. First, we need to convert the octal number into a binary number system, and then this binary number system will be converted into a hexadecimal number system.

Code -

```#include <stdio.h>
#include<string.h>
int main()
{
int octaltobinary[]={0,1,10,11,100,101,110,111};
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:
break;
case 1:
break;
case 10:
break;
case 11:
break;
case 100:
break;
case 101:
break;
case 110:
break;
case 111:
break;
case 1000:
break;
case 1001:
break;
case 1010:
break;
case 1011:
break;
case 1100:
break;
case 1101:
break;
case 1110:
break;
case 1111:
break;
}
len=len+1;
binary /= 10000;
}
for(int i=len-1;i>=0;i--)
{
k++;
}
hex[len]='\0';
for(int i=0; hex[i]!='\0';i++)
{
printf("%c",hex[i]);
}

return 0;
}  ```

Output -

```Enter a octal number 123
The Binary representation is 100111

Tags:

### Related Posts

See All

#### Program of Random Number in C || C Programs

Code - #include <stdio.h> #include <conio.h> #include <stdlib.h> void main() { // use rand() function to generate the number printf (" The random number is: %d", rand()); pri

#### Getchar() function in C || C Programs

Code - #include <stdio.h> #include <conio.h> void main() { char c; printf ("\n Enter a character \n"); c = getchar(); // get a single character printf(" You have passed ")