# C program to convert Octal to Hexadecimal number system

Write a C program to input octal number from user and convert to hexadecimal number system. How to convert from octal number system to hexadecimal number system in C program. Logic to convert octal to hexadecimal number system in C programming.

Example

Input

`Input octal number: 175`

Output

`Hexadecimal number: 7D`

## Required knowledge

Must know -

### Octal number system

Octal number system is a base 8 number system. It uses 8 symbols to represent all its numbers i.e. `01234567`

Hexadecimal number system is a base 16 number system. It uses 16 symbols to represent all its numbers i.e. `0123456789ABCDEF`

## Logic to convert octal to hexadecimal

There is no direct conversion from octal to hexadecimal number system. You first need to convert the given octal to binary number system. Then binary number system is converted to hexadecimal number system.

Below is the step by step descriptive logic to convert octal to hexadecimal.

1. Convert the given octal number to binary number system.
2. Extract binary bits in a group of 4 bit starting from right side.
3. Write the corresponding hexadecimal of extracted 4 binary bits. 000000
100011
200102
300113
401004
501015
601106
701117
810008
910019
101010A
111011B
121100C
131101D
141110E
151111F

## Program to convert octal to hexadecimal

``````/**
* C program to convert Octal to Hexadecimal number system
*/

#include <stdio.h>

int main()
{
int OCTALVALUES[] = {0, 1, 10, 11, 100, 101, 110, 111};

long long octal, tempOctal, binary, place;
char hex = "";
int rem;

place = 1;
binary = 0;

/* Input octal number from user */
printf("Enter any octal number: ");
scanf("%lld", &octal);
tempOctal = octal;

/*
* Octal to binary conversion
*/
while(tempOctal > 0)
{
rem = tempOctal % 10;
binary = (OCTALVALUES[rem] * place) + binary;
tempOctal /= 10;

place *= 1000;
}

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

binary /= 10000;
}

strrev(hex);

printf("Octal number: %lld\n", octal);

return 0;
}``````

Output

```Enter any octal number: 125715
Octal number: 125715 