hexadecimal to octal Algorithm

The hexadecimal to octal algorithm is a method for converting a number represented in base-16 (hexadecimal) notation to base-8 (octal) notation. Hexadecimal is a positional numeral system that uses 16 distinct symbols to represent numbers - the digits 0-9, and the letters A-F. Octal, on the other hand, is a positional numeral system that uses 8 distinct symbols, the digits 0-7, to represent numbers. Both hexadecimal and octal are widely used in computer science and computing applications because they provide a more human-friendly way of representing binary, or base-2, numbers, which form the foundation of digital computing systems. The algorithm for converting a hexadecimal number to an octal number can be broken down into two main steps. Firstly, the hexadecimal number is converted to its binary equivalent by replacing each hex digit with its corresponding 4-bit binary representation. For example, the hexadecimal number '2A3' would be represented as '0010 1010 0011' in binary. Secondly, the binary number is converted to its octal equivalent by grouping the binary digits into sets of three, starting from the least significant bit (rightmost bit) and moving towards the most significant bit (leftmost bit), and replacing each 3-bit group with its corresponding octal digit. If the leftmost group has less than three binary digits, zero-padding is added to complete the group. Continuing the previous example, the binary number '0010 1010 0011' would be grouped as '010 101 000 011', and finally converted to the octal number '5243'.
/* 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;

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

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

    /* Hexadecimal to binary conversion */
    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;

    /* Binary to octal conversion */
    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("Hexadecimal number = %s\n", hex);
    printf("Octal number = %lld", octal);

    return 0;
} 

LANGUAGE:

DARK MODE: