C program to convert Octal to Hexadecimal number system

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

Example:
Input Octal number: (175)8
Output Hexadecimal number: (7D)16

Required knowledge:

Basic C programming, For loop

Octal number system

Octal number system is a base 8 number system. It uses 8 symbols to represent all its numbers i.e. 0 1 2 3 4 5 6 7

Hexadecimal number system

Hexadecimal number system is a base 16 number system. It uses 16 symbols to represent all its numbers i.e. 0 1 2 3 4 5 6 7 8 9 A B C D E F

Algorithm to convert Octal to Hexadecimal:

Conversion from octal to hexadecimal number system is little tricky. What you need to do is:
Step 1: Convert the octal number system to binary number system.
Step 2: Separate converted binary number in the group of 4 binary digits.
Step 3: Write hexadecimal value of each grouped binary digit.
Octal to hexadecimal conversion

Binary to Hexadecimal conversion table
Decimal Binary Hexadecimal
0 0000 0
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F

Program:

/**
 * 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[65] = "";
    int rem;

    place = 1;
    binary = 0;
    
    /* 
     * Reads octal number from user
     */
    printf("Enter any octal number: ");
    scanf("%lld", &octal);
    tempOctal = octal;

    /*
     * Finds binary value of octal number
     */
    while(tempOctal!=0)
    {
        rem = tempOctal % 10;
        binary = (OCTALVALUES[rem] * place) + binary;
        tempOctal /= 10;

        place *= 1000;
    }
    
    /* 
     * Finds hexadecimal of binary value converted in above step
     */
    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("\nOctal number: %lld\n", octal);
    printf("Hexadecimal number: %s", hex);

    return 0;
}
Output
Enter any octal number: 125715

Octal number: 125715
Hexadecimal number: ABCD

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment