Program to Convert Hexadecimal to Octal

Program to Convert Hexadecimal to Octal

  • Write a program to Convert Hexadecimal to Octal in C
  • Write a program to Convert Hexadecimal to Octal in C++
  • Write a program to Convert Hexadecimal to Octal in Python
  • Write a program to Convert Hexadecimal to Octal in PHP
  • Write a program to Convert Hexadecimal to Octal in Java
  • Write a program to Convert Hexadecimal to Octal in Java Script
  • Write a program to Convert Hexadecimal to Octal in C#

Explanation:

  1. Convert Hexadecimal to Binary:
    • Each hexadecimal digit corresponds to a 4-bit binary equivalent. Use a lookup table to convert each hexadecimal digit into binary.
  2. Group Binary into 3-bit Blocks:
    • Octal uses 3-bit groups. Group the binary digits into sets of 3, starting from the right. Add leading zeros if the number of binary digits is not a multiple of 3.
  3. Convert Each 3-bit Group to Octal:
    • Each 3-bit binary group corresponds to a single octal digit. Use a lookup table or direct conversion logic to find the octal value for each group.

Example Conversion:

Hexadecimal: 2F

  1. Convert Hexadecimal to Binary:
    • 2 → 0010
    • F → 1111
    • Combined Binary: 00101111
  2. Group Binary into 3-bit Blocks:
    • From right to left: 00101111 → 000 101 111(add leading zeros if necessary).
  3. Convert Each 3-bit Group to Octal:
    • 000 → 0
    • 101 → 5
    • 111 → 7

Resulting Octal: 057

Program to Convert Hexadecimal to Octal

#include <stdio.h>
#include <math.h>

int hexToDecimal(char hex[]) {
    int decimal = 0, base = 1;
    for (int i = strlen(hex) - 1; i >= 0; i--) {
        if (hex[i] >= '0' && hex[i] <= '9') {
            decimal += (hex[i] - 48) * base;
        } else if (hex[i] >= 'A' && hex[i] <= 'F') {
            decimal += (hex[i] - 55) * base;
        } else if (hex[i] >= 'a' && hex[i] <= 'f') {
            decimal += (hex[i] - 87) * base;
        }
        base *= 16;
    }
    return decimal;
}

void decimalToOctal(int decimal) {
    int octal[32], i = 0;
    while (decimal > 0) {
        octal[i++] = decimal % 8;
        decimal /= 8;
    }
    printf("Octal: ");
    for (int j = i - 1; j >= 0; j--) {
        printf("%d", octal[j]);
    }
    printf("\n");
}

int main() {
    char hex[100];
    printf("Enter a hexadecimal number: ");
    scanf("%s", hex);
    int decimal = hexToDecimal(hex);
    decimalToOctal(decimal);
    return 0;
}

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

int hexToDecimal(string hex) {
    int decimal = 0, base = 1;
    for (int i = hex.length() - 1; i >= 0; i--) {
        if (hex[i] >= '0' && hex[i] <= '9') {
            decimal += (hex[i] - '0') * base;
        } else if (hex[i] >= 'A' && hex[i] <= 'F') {
            decimal += (hex[i] - 'A' + 10) * base;
        } else if (hex[i] >= 'a' && hex[i] <= 'f') {
            decimal += (hex[i] - 'a' + 10) * base;
        }
        base *= 16;
    }
    return decimal;
}

void decimalToOctal(int decimal) {
    string octal = "";
    while (decimal > 0) {
        octal = to_string(decimal % 8) + octal;
        decimal /= 8;
    }
    cout << "Octal: " << octal << endl;
}

int main() {
    string hex;
    cout << "Enter a hexadecimal number: ";
    cin >> hex;
    int decimal = hexToDecimal(hex);
    decimalToOctal(decimal);
    return 0;
}

def hex_to_octal(hexadecimal):
    decimal = int(hexadecimal, 16)
    return oct(decimal).replace("0o", "")

hexadecimal = input("Enter a hexadecimal number: ")
print("Octal:", hex_to_octal(hexadecimal))

<?php
function hexToOctal($hex) {
    $decimal = hexdec($hex);
    return decoct($decimal);
}

$hex = readline("Enter a hexadecimal number: ");
echo "Octal: " . hexToOctal($hex) . "\n";
?>

<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950"><div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-java">import java.util.Scanner;

public class HexadecimalToOctal {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a hexadecimal number: ");
        String hex = sc.nextLine();
        int decimal = Integer.parseInt(hex, 16);
        String octal = Integer.toOctalString(decimal);
        System.out.println("Octal: " + octal);
    }
}

function hexToOctal(hex) {
    const decimal = parseInt(hex, 16);
    return decimal.toString(8);
}

const hex = prompt("Enter a hexadecimal number: ");
console.log("Octal:", hexToOctal(hex));

using System;

class HexadecimalToOctal {
    static void Main() {
        Console.Write("Enter a hexadecimal number: ");
        string hex = Console.ReadLine();
        int decimalValue = Convert.ToInt32(hex, 16);
        string octal = Convert.ToString(decimalValue, 8);
        Console.WriteLine("Octal: " + octal);
    }
}

List of All Programs