C Program to Convert Octal to Hexadecimal

In this article, we will learn 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 does the same job.

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

then refer to the octal to hexadecimal conversion step-by-step process. Now let's move on to the program.

Octal to hexadecimal in C

To convert an octal number to a hexadecimal number in C programming, you have to ask the user to enter any octal number. Then convert it to hexadecimal and display the hex value as output. The question is, Write a program in C that converts any given octal number entered by the 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 in the Code::Blocks IDE, 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 the 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

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

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

Octal to Hexadecimal in C using a User-Defined Function

Let's write the same program for the same purpose, but this time with user-defined functions. The question is: "Write a program in C that converts octal to hexadecimal using a 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 variables i and count as static variables, so that they can hold or remember their previous values. We have not initialized both the static variables with 0, because by default, static variables get initialized with 0 as their initial value automatically. We have declared both static variables and a character array hexnum[] as global variables to make all three variables known to both the main() and OctToHex() functions.

The same program in different languages

C Quiz


« Previous Program Next Program »


Liked this post? Share it!