C Program to Convert Octal to Hexadecimal

In this tutorial, we will learn about how to create a program in C that converts any given octal number into its equivalent hexadecimal value. At last we have also created a program using a user-defined function OctToHex() that also does the same job.

But before going through the program, if you are not aware of

  • Octal Number
  • Hexadecimal Number
  • Octal to Hexadecimal Conversion Process

then refer to Octal to Hexadecimal conversion step by step process. Now let's move on to the program.

Octal to Hexadecimal in C

To convert octal number to hexadecimal number in C programming, you have to ask from user to enter any octal number. Then convert it into hexadecimal and display hex value as output. The question is, Write a program in C that converts any given octal number by user (at run-time) to its equivalent hexadecimal value. The answer to this question is given below:

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    int octnum, rev=0, rem, count=0, hex=0, mul=1, i=0, k=0;
    char binnum[40] = "", hexnum[40];
    printf("Enter any Octal Number: ");
    scanf("%d", &octnum);
    while(octnum!=0)
    {
        rem = octnum%10;
        if(rem>7)
        {
            count++;
            break;
        }
        rev = (rev*10) + rem;
        octnum = octnum/10;
    }
    if(count==0)
    {
        octnum = rev;
        while(octnum!=0)
        {
            rem = octnum%10;
            switch(rem)
            {
                case 0: strcat(binnum, "000");
                    break;
                case 1: strcat(binnum, "001");
                    break;
                case 2: strcat(binnum, "010");
                    break;
                case 3: strcat(binnum, "011");
                    break;
                case 4: strcat(binnum, "100");
                    break;
                case 5: strcat(binnum, "101");
                    break;
                case 6: strcat(binnum, "110");
                    break;
                case 7: strcat(binnum, "111");
                    break;
            }
            octnum = octnum/10;
        }
        while(binnum[k]!='\0')
            k++;
        count=1;
        k--;
        while(k>=0)
        {
            if(binnum[k]=='0')
                rem = 0;
            else
                rem = 1;
            hex = hex + (rem*mul);
            if(count%4==0)
            {
                if(hex<10)
                    hexnum[i] = hex+48;
                else
                    hexnum[i] = hex+55;
                mul = 1;
                hex = 0;
                count = 1;
                i++;
            }
            else
            {
                mul = mul*2;
                count++;
            }
            k--;
        }
        if(count!=1)
            hexnum[i] = hex+48;
        if(count==1)
            i--;
        printf("\nEquivalent Hexadecimal Value = ");
        count = 0;
        for(i=i; i>=0; i--)
        {
            if(hexnum[i]=='0' && count==0)
            {
                count++;
                continue;
            }
            else
                printf("%c", hexnum[i]);
        }
    }
    else
        printf("\nInvalid Octal Digit %d", rem);
    getch();
    return 0;
}

As the above program was written under Code::Blocks IDE, therefore after successful build and run, here is the first snapshot of the sample run:

c program convert octal to hexadecimal

Now supply any octal number say 1452 as input and press ENTER key to see its hexadecimal equivalent as shown in the second snapshot of the sample run:

c octal to hexadecimal

Here is the final snapshot of another sample run:

octal to hexadecimal conversion c

Program Explained

  • Receive any octal number from user at run-time say 1452 as input
  • Now reverse the given octal number and initialize its reverse to the original number say octnum itself
  • Here we have reverse the number because using modulous (%) or remainder operator we will get the last digit. For example, while applying modulous operator in a way like 1452%10, then we will get 2 as result, and 2 is the last digit, but after reversing the number, it will become 2541. And then after applying modulous operator in same way like 2541%10, we will get 1 as result, and 1 is the first digit of original number say 1452
  • While reversing the number, we have checked whether any digit is greater than 7 or not. As in octal number system, all the digit must be less than or equal to 7. But if any digit is greater than 7, then we have incremented a variable say count (it is initialized with 0 at start of the program), so that we can check after exiting from the while loop that it holds its original value (0) or not, if it holds, then we have to proceed to convert the number from octal to hexadecimal, otherwise we have to print a message like you have entered an invalid octal digit
  • Now if count holds its original value, then program flow goes inside the if block
  • And rev (reverse of octal number) or 2541 gets initialized to octnum
  • Then we have created a while loop to convert octal number to binary
  • The condition of the while loop octnum!=0 or 2541!=0 evaluates to be true, and program flow goes inside the loop
  • There, octnum%10 or 2541%10 or 1 gets initialized to rem
  • Using switch case, we have checked, what value present inside the variable rem
  • Whatever the value in rem, concatenate it to binnum character array
  • Here we have used strcat() function of string.h library, so that the binary equivalent (in string form) of each octal digit gets concatenated into binnum character array one by one
  • After concatenation, octnum/10 or 2541/10 or 254 gets initialized to octnum
  • And the program flow goes back to the condition of the loop, there octnum!=0 or 254!=0 evaluates to be true, and program flow again goes inside the loop and do the same operation as told above until the value of octnum becomes 0
  • After exiting from the while loop, we have binnum character array that holds 001100101010
  • Here 001, 100, 101, and 010 are the binary equivalent of 1, 4, 5, and 2
  • Now we have used while loop, to count the length of binnum character array, that how many character present inside binnum array
  • The length of array binnum initialized to a variable say k
  • The value of k gets decremented as indexing in array starts with 0
  • Now we have binary equivalent of each octal digit
  • It's time to make 4-4 binary pair from right side and convert it into its hexadecimal equivalent
  • To do this, we have starts from last index of binnum array
  • At first run of the while loop, the condition k>=0 or 8>=0 evaluates to be true, therefore program flow goes inside the loop
  • Inside the loop, we have checked what the character present at current index of the array binnum, if it is 0, then initialized 0 to rem, otherwise initialized 1 to rem
  • And do the same procedure as told in binary to hexadecimal in C tutorial
  • After doing all the above steps, we only have to print all the hexadecimal digit one by one
  • Here we have used if statement inside the for loop in printing hexadecimal digit, so that any zero (0) present at beginning of the hexadecimal number gets skipped

Note - You can also approach the second option, that is octal to decimal and then decimal to hexadecimal. Because the second option is little easier to create and understand than the first one. Therefore, the first option is used here that converts octal to binary and then binary to hexadecimal.

You can follow both the article (used for second option) to create the program with yourself. I'm sure, that will be very easy for you after understanding the first option as created above.

Octal to Hexadecimal in C using User-defined Function

Let's create the same purpose program, but this time using user-defined function. The question is, Write a program in C that converts Octal to Hexadecimal using user-defined function named OctToHex(). The answer to this question is:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void OctToHex(int oct);
static int count, i;
char hexnum[40];
int main()
{
    int octnum;
    printf("Enter any Octal Number: ");
    scanf("%d", &octnum);
    OctToHex(octnum);
    if(count==0)
        printf("\nYou've entered an invalid octal digit");
    else
    {
        printf("\nEquivalent Hexadecimal Value = ");
        count = 0;
        for(i=i; i>=0; i--)
        {
            if(hexnum[i]=='0' && count==0)
            {
                count++;
                continue;
            }
            else
                printf("%c", hexnum[i]);
        }
    }
    getch();
    return 0;
}
void OctToHex(int oct)
{
    int rev=0, rem, hex=0, mul=1, k=0;
    char binnum[40] = "";
    while(oct!=0)
    {
        rem = oct%10;
        if(rem>7)
        {
            count++;
            break;
        }
        rev = (rev*10) + rem;
        oct = oct/10;
    }
    if(count==0)
    {
        oct = rev;
        while(oct!=0)
        {
            rem = oct%10;
            switch(rem)
            {
                case 0: strcat(binnum, "000");
                    break;
                case 1: strcat(binnum, "001");
                    break;
                case 2: strcat(binnum, "010");
                    break;
                case 3: strcat(binnum, "011");
                    break;
                case 4: strcat(binnum, "100");
                    break;
                case 5: strcat(binnum, "101");
                    break;
                case 6: strcat(binnum, "110");
                    break;
                case 7: strcat(binnum, "111");
                    break;
            }
            oct = oct/10;
        }
        while(binnum[k]!='\0')
            k++;
        count=1;
        k--;
        while(k>=0)
        {
            if(binnum[k]=='0')
                rem = 0;
            else
                rem = 1;
            hex = hex + (rem*mul);
            if(count%4==0)
            {
                if(hex<10)
                    hexnum[i] = hex+48;
                else
                    hexnum[i] = hex+55;
                mul = 1;
                hex = 0;
                count = 1;
                i++;
            }
            else
            {
                mul = mul*2;
                count++;
            }
            k--;
        }
        if(count!=1)
            hexnum[i] = hex+48;
        if(count==1)
            i--;
    }
    else
        count=0;
}

Here is the final snapshot of the sample run:

c octal to hexadecimal using function

Here we have declared the variable i and count as static variable, so that it can hold or remembers its previous values. We have not initialized both the static variable with 0, because by default static variable gets initialized 0 as its initial value automatically. We have declared both static variables and a character array hexnum[] as global variables to make all the three variables known to both the function say main() and OctToHex().

Same Program in Other Languages

C Online Test


« Previous Program Next Program »