Program to Convert Octal to Hexadecimal

Program to Convert Octal to Hexadecimal

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

Explanation:

Since octal and binary have a direct link, the most effective way to convert an octal number (base 8) to its hexadecimal equivalent (base 16) is to convert it first to binary and then to hexadecimal. The conversion is simple thanks to this two-step procedure.

Steps for Octal to Hexadecimal Conversion:

  1. Convert the octal number to binary.
    • Since 1 octal digit corresponds to exactly 3 binary digits, convert each octal digit to its 3-bit binary equivalent.
  2. Group the binary digits into sets of 4 (starting from the right).
    • Add leading zeros if necessary to make the number a multiple of 4 bits.
  3. Convert each group of 4 binary digits to a hexadecimal digit.
    • Each group of 4 binary digits corresponds to a single hexadecimal digit.

Example Conversion:

Convert 3458​ to hexadecimal:

Step 1: Convert Octal to Binary

Each octal digit maps to a 3-bit binary equivalent:

  • 3 (octal) → 011 (binary)
  • 4 (octal) → 100 (binary)
  • 5 (octal) → 101 (binary)

So, 3458 becomes 0111001012.

Step 2: Group the Binary Digits into Sets of 4

Now, group the binary digits into sets of 4 (starting from the right):

  • 0111001012​ → 0001 ∣ 1100 ∣ 0101

Step 3: Convert Binary Groups to Hexadecimal

Now, convert each binary group to its hexadecimal equivalent:

  • 00012​ → 1
  • 11002​ → C
  • 01012​ → 5

So, the hexadecimal equivalent of 3458​ is 1C516.

Program to Convert Octal to Hexadecimal

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

int octalToDecimal(int octal) {
    int decimal = 0, base = 1;
    while (octal > 0) {
        int remainder = octal % 10;
        decimal += remainder * base;
        base *= 8;
        octal /= 10;
    }
    return decimal;
}

void decimalToHexadecimal(int decimal) {
    char hex[100];
    int i = 0;
    while (decimal != 0) {
        int temp = decimal % 16;
        if (temp < 10)
            hex[i] = temp + 48;
        else
            hex[i] = temp + 55;
        decimal /= 16;
        i++;
    }
    for (int j = i - 1; j >= 0; j--)
        printf("%c", hex[j]);
}

int main() {
    int octal;
    printf("Enter an octal number: ");
    scanf("%d", &octal);
    int decimal = octalToDecimal(octal);
    printf("Hexadecimal: ");
    decimalToHexadecimal(decimal);
    return 0;
}

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

int octalToDecimal(int octal) {
    int decimal = 0, base = 1;
    while (octal > 0) {
        int remainder = octal % 10;
        decimal += remainder * base;
        base *= 8;
        octal /= 10;
    }
    return decimal;
}

void decimalToHexadecimal(int decimal) {
    string hex = "";
    char hexChars[] = "0123456789ABCDEF";
    while (decimal > 0) {
        hex = hexChars[decimal % 16] + hex;
        decimal /= 16;
    }
    cout << hex;
}

int main() {
    int octal;
    cout << "Enter an octal number: ";
    cin >> octal;
    int decimal = octalToDecimal(octal);
    cout << "Hexadecimal: ";
    decimalToHexadecimal(decimal);
    return 0;
}

def octal_to_hexadecimal(octal):
    decimal = int(str(octal), 8)
    return hex(decimal).replace("0x", "").upper()

octal = input("Enter an octal number: ")
print("Hexadecimal:", octal_to_hexadecimal(octal))

<?php
function octalToHexadecimal($octal) {
    $decimal = octdec($octal);
    return strtoupper(dechex($decimal));
}

$octal = readline("Enter an octal number: ");
echo "Hexadecimal: " . octalToHexadecimal($octal) . "\n";
?>

import java.util.Scanner;

public class OctalToHexadecimal {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter an octal number: ");
        String octal = sc.nextLine();
        int decimal = Integer.parseInt(octal, 8);
        String hexadecimal = Integer.toHexString(decimal).toUpperCase();
        System.out.println("Hexadecimal: " + hexadecimal);
    }
}

function octalToHexadecimal(octal) {
    const decimal = parseInt(octal, 8);
    return decimal.toString(16).toUpperCase();
}

const octal = prompt("Enter an octal number: ");
console.log("Hexadecimal:", octalToHexadecimal(octal));

using System;

class OctalToHexadecimal {
    static void Main() {
        Console.Write("Enter an octal number: ");
        string octal = Console.ReadLine();
        int decimalValue = Convert.ToInt32(octal, 8);
        string hexadecimal = decimalValue.ToString("X");
        Console.WriteLine("Hexadecimal: " + hexadecimal);
    }
}

List of All Programs