Program to Convert Hexadecimal to Octal

Given a Hexadecimal number, the task is to convert it into an Octal number.

Examples:

Input: Hexadecimal = 1AC
Output: Binary = 0654
Explanation:
    Equivalent binary value of 1: 0001
    Equivalent binary value of A: 1010
    Equivalent binary value of C: 1100

    Grouping in terms of 3: 000 110 101 100

    Equivalent octal value of 000: 0
    Equivalent octal value of 110: 6
    Equivalent octal value of 101: 5
    Equivalent octal value of 100: 4

Input: Hexadecimal = 5D1F
Output: Octal = 56437

Approach:
A hexadecimal number is a positional numeral system with a radix, or base, of 16 and uses sixteen distinct symbols.

An octal number or oct for short is the base-8 number and uses the digits 0 to 7. Octal numerals can be made from binary numerals by grouping consecutive binary digits into groups of three (starting from the right).

Steps of Conversion

  1. Find the equivalent binary number for each digit of the given hexadecimal number. Add 0’s to the left if any of the binary equivalent is shorter than 4 bits.
  2. Separate the binary digits into groups, each containing 3 bits or digits from right to left. Add 0s to the left, if the last group contains less than 3 bits.
  3. Find the octal equivalent for each binary group.

Below is the implementation of the above approach:

C

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to convert
// Hexadecimal to Octal
  
#include <math.h>
#include <stdio.h>
#include <string.h>
  
// Function to convert HexaDecimal to Binary
long long int hex_to_bin(char hex[])
{
    long long int bin, place;
    int i = 0, rem, val;
  
    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;
    }
  
    return bin;
}
  
// Function to convert Binary to Octal
long long int bin_to_oct(long long bin)
{
    long long int octal, place;
    int i = 0, rem, val;
  
    octal = 0ll;
    place = 0ll;
  
    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;
    }
  
    return octal;
}
  
// Function to Convert
// Hexadecimal Number to Octal Number
long long int hex_to_oct(char hex[])
{
    long long int octal, bin;
  
    // convert HexaDecimal to Binary
    bin = hex_to_bin(hex);
  
    // convert Binary to Octal
    octal = bin_to_oct(bin);
  
    return octal;
}
  
// driver code
int main()
{
  
// Get the hexadecimal number
    char hex[20] = "1AC";
  
// convert hexadecimal to octal
    printf("Equivalent Octal Value = %lld",
 hex_to_oct(hex));
  
    return 0;
}

chevron_right




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.