C program to convert Hexadecimal to Octal number system

Write a C program to read Hexadecimal number from user and convert it to Octal number system. How to convert Hexadecimal number system to Octal number system in C.

Example:
Input hexadecimal number: (1A)16
Output octal number: (32)8

Required knowledge:

Basic C programming, For loop

Hexadecimal number system

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

Octal number system

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

Algorithm to convert Hexadecimal to Octal:

There isn't any easy and direct conversion algorithm from hexadecimal to octal number system. To convert from hexadecimal to octal number system :
Step 1: Convert the hexadecimal number to binary number system.
Step 2: Group the converted binary number in the group of 3 digits.
Step 3: Write the corresponding octal value for the each grouped binary number.
Hexadecimal to Octal conversion

Binary to Octal conversion table
Decimal Binary Octal
0 000 0
1 001 1
2 010 2
3 011 3
4 100 4
5 101 5
6 110 6
7 111 7


Program:

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

#include <stdio.h>

int main()
{
    char hex[17];
    long long octal, bin, place;
    int i = 0, rem, val;

    /*
     * Reads hexadecimal number from user
     */
    printf("Enter any hexadecimal number: ");
    gets(hex);

    octal = 0ll;
    bin = 0ll;
    place = 0ll;

    /*
     * Finds binary of each hexadecimal digit
     */
    for(i=0; hex[i]!='\0'; i++)
    {
        bin = bin * place;

        switch(hex[i])
        {
            case '0':
                bin += 0;
                break;
            case '1':
                bin += 1;
                break;
            case '2':
                bin += 10;
                break;
            case '3':
                bin += 11;
                break;
            case '4':
                bin += 100;
                break;
            case '5':
                bin += 101;
                break;
            case '6':
                bin += 110;
                break;
            case '7':
                bin += 111;
                break;
            case '8':
                bin += 1000;
                break;
            case '9':
                bin += 1001;
                break;
            case 'a':
            case 'A':
                bin += 1010;
                break;
            case 'b':
            case 'B':
                bin += 1011;
                break;
            case 'c':
            case 'C':
                bin += 1100;
                break;
            case 'd':
            case 'D':
                bin += 1101;
                break;
            case 'e':
            case 'E':
                bin += 1110;
                break;
            case 'f':
            case 'F':
                bin += 1111;
                break;
            default:
                printf("Invalid hexadecimal input.");
        }

        place = 10000;
    }

    place = 1;

    /*
     * Finds octal equivalent of each 3 digits of converted binary
     */
    while(bin!=0)
    {
        rem = bin %1000;

        switch(rem)
        {
            case 0:
                val = 0;
                break;
            case 1:
                val = 1;
                break;
            case 10:
                val = 2;
                break;
            case 11:
                val = 3;
                break;
            case 100:
                val = 4;
                break;
            case 101:
                val = 5;
                break;
            case 110:
                val = 6;
                break;
            case 111:
                val = 7;
                break;
        }

        octal = (val * place) + octal;
        bin /= 1000;

        place *= 10;
    }

    printf("\nHexadecimal number = %s\n", hex);
    printf("Octal number = %lld", octal);

    return 0;
} 
Output
Enter any hexadecimal number: fff

Hexadecimal number = fff
Octal number = 7777

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