Program to Convert Octal to Hexadecimal

25 May, 2022
Given an Octal number, the task is to convert it into a Hexadecimal number.
Examples: 
Input:  47 
Output: 27
Explanation:
Decimal value of 47 is = (7 * 1) + (4 * 8) = 39

Now, convert this number to hexadecimal
39/16 -> quotient = 2, remainder = 7
2/16 ->  quotient = 0, remainder = 2

So, the equivalent hexadecimal number is = 27

Input: 235
Output:  9d
Approach: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).
A Hexadecimal Number is a positional numeral system with a radix, or base, of 16 and uses sixteen distinct symbols. It may be a combination of alphabets and numbers. It uses numbers from 0 to 9 and alphabets A to F.
Steps of Conversion:The simplest way is to convert the octal number into a decimal, then the decimal into hexadecimal form. 
Write the powers of 8 (1, 8, 64, 512, 4096, and so on) beside the octal digits from bottom to top.Multiply each digit by its power.Add up the answers. This is the decimal solution.Divide the decimal number by 16.Get the integer quotient for the next iteration (if the number will not divide equally by 16, then round down the result to the nearest whole number).Keep a note of the remainder, it should be between 0 and 15.Repeat the steps from step 4. until the quotient is equal to 0.Write out all the remainders, from bottom to top.Convert any remainders bigger than 9 into hex letters. This is the hex solution.
Write the powers of 8 (1, 8, 64, 512, 4096, and so on) beside the octal digits from bottom to top.
Multiply each digit by its power.
Add up the answers. This is the decimal solution.
Divide the decimal number by 16.
Get the integer quotient for the next iteration (if the number will not divide equally by 16, then round down the result to the nearest whole number).
Keep a note of the remainder, it should be between 0 and 15.
Repeat the steps from step 4. until the quotient is equal to 0.
Write out all the remainders, from bottom to top.
Convert any remainders bigger than 9 into hex letters. This is the hex solution.
For example, if the given octal number is 5123:
Then the decimal number (2560 + 64 + 16 + 3) is: 2643
Finally, the hexadecimal number is: a53
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program to convert Octal// to Hexadecimal#include<bits/stdc++.h>using namespace std; // Function to convert octal to decimalint octalToDecimal(int n){    int num = n;    int dec_value = 0;         // Initializing base value    // to 1, i.e 8^0    int base = 1;       int temp = num;    while (temp)    {                 // Extracting last digit        int last_digit = temp % 10;        temp = temp / 10;                 // Multiplying last digit with        // appropriate base value and        // adding it to dec_value        dec_value += last_digit * base;           base = base * 8;    }    return dec_value;} // Function to convert decimal// to hexadecimalstring decToHexa(int n){         // char array to store    // hexadecimal number    char hexaDeciNum[100];         // counter for hexadecimal    // number array    int i = 0;    while(n != 0)    {                    // Temporary variable to        // store remainder        int temp  = 0;                   // Storing remainder in        // temp variable.        temp = n % 16;                   // Check if temp < 10        if (temp < 10)        {            hexaDeciNum[i] = temp + 48;            i++;        }        else        {            hexaDeciNum[i] = temp + 87;            i++;        }        n = n / 16;    }         string ans = "";           // Printing hexadecimal number array    // in reverse order    for(int j = i - 1; j >= 0; j--)    {        ans += hexaDeciNum[j];    }    return ans;} // Driver Codeint main(){    string hexnum;    int decnum, octnum;         // Taking 5123 as an example of    // Octal Number.    octnum = 5123;         // Convert Octal to Decimal    decnum = octalToDecimal(octnum);         // Convert Decimal to Hexadecimal    hexnum = decToHexa(decnum);         cout << "Equivalent Hexadecimal Value = "         << hexnum << endl;} // This code is contributed by pratham76
// Java Program to Convert Octal// to Hexadecimal import java.util.Scanner; public class JavaProgram {    public static void main(String args[])    {        String octnum, hexnum;        int decnum;        Scanner scan = new Scanner(System.in);                 // Taking 5123 as an example of        // Octal Number.        octnum = "5123";                 // Convert Octal to Decimal        decnum = Integer.parseInt(octnum, 8);                 // Convert Decimal to Hexadecimal        hexnum = Integer.toHexString(decnum);         System.out.print("Equivalent Hexadecimal Value = "                                                + hexnum);    }}
# Python3 program to convert octal# to hexadecimal # Taking 5123 as an example of# octal numberoctnum = "5123" # Convert octal to decimaldecnum = int(octnum, 8) # Convert decimal to hexadecimalhexadecimal = hex(decnum).replace("0x", "") # Printing the hexadecimal valueprint("Equivalent Hexadecimal Value =", hexadecimal) # This code is contributed by virusbuddah_
// C# Program to Convert Octal// to Hexadecimalusing System;class GFG{     // Function to convert octal// to decimalstatic int octalToDecimal(int n){  int num = n;  int dec_value = 0;   // Initializing base value   // to 1, i.e 8^0  int b_ase = 1;   int temp = num;  while (temp > 0)   {    // Extracting last digit    int last_digit = temp % 10;    temp = temp / 10;     // Multiplying last digit     // with appropriate base     // value and adding it to     // dec_value    dec_value += last_digit * b_ase;     b_ase = b_ase * 8;  }     return dec_value;} // Function to convert decimal// to hexadecimalstatic string decToHexa(int n){   // char array to store   // hexadecimal number  char []hexaDeciNum = new char[100];   // counter for hexadecimal  // number array  int i = 0;  string ans = "";   while(n != 0)  {     // temporary variable to     // store remainder    int temp = 0;     // storing remainder    // in temp variable.    temp = n % 16;     // check if temp < 10    if(temp < 10)    {      hexaDeciNum[i] = (char)(temp + 48);      i++;    }    else    {      hexaDeciNum[i] = (char)(temp + 87);      i++;    }     n = n / 16;  }   for(int j = i - 1; j >= 0; j--)  {    ans += hexaDeciNum[j];  }     return ans;}   // Driver codepublic static void Main(string []args){  string octnum, hexnum;  int decnum;   // Taking 5123 as an  // example of Octal Number.  octnum = "5123";   // Convert Octal to Decimal  decnum = octalToDecimal(Int32.Parse(octnum));   // Convert Decimal to Hexadecimal  hexnum = decToHexa(decnum);   Console.Write("Equivalent Hexadecimal Value = " +                 hexnum);}} // This code is contributed by rutvik_56
// JavaScript program to convert Octal// to Hexadecimal  // Function to convert octal to decimalfunction octalToDecimal( n){    var num = n;    var dec_value = 0;         // Initializing base value    // to 1, i.e 8^0    var base = 1;       var temp = num;    while (temp > 0)    {        // Extracting last digit        var last_digit = temp % 10;        temp = Math.floor(temp / 10);                 // Multiplying last digit with        // appropriate base value and        // adding it to dec_value        dec_value += last_digit * base;           base = base * 8;    }    return dec_value;} // Function to convert decimal// to hexadecimalfunction decToHexa( n){         // char array to store    // hexadecimal number    var hexaDeciNum = new Array(100);         // counter for hexadecimal    // number array    var i = 0;    while(n != 0)    {                    // Temporary variable to        // store remainder        var temp  = 0;                   // Storing remainder in        // temp variable.        temp = n % 16;                   // Check if temp < 10        if (temp < 10)        {            hexaDeciNum[i] = temp + 48;            i++;        }        else        {            hexaDeciNum[i] = temp + 87;            i++;        }        n = Math.floor(n / 16);    }         var ans = "";           // Printing hexadecimal number array    // in reverse order    for(var j = i - 1; j >= 0; j--)    {        ans += String.fromCharCode(hexaDeciNum[j]);    }    return ans;} // Driver Codevar hexnum;var decnum, octnum;     // Taking 5123 as an example of// Octal Number.octnum = 5123;     // Convert Octal to Decimaldecnum = octalToDecimal(octnum);     // Convert Decimal to Hexadecimalhexnum = decToHexa(decnum); console.log("Equivalent Hexadecimal Value = " + hexnum);  // This code is contributed by phasing17
Equivalent Hexadecimal Value = a53
 
virusbuddha
rutvik_56
pratham76
phasing17
number-theory
Java Programs
Mathematical
number-theory
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Iterate Over the Characters of a String in Java
How to Convert Char to String in Java?
How to Get Elements By Index from HashSet in Java?
Java Program to Write into a File
How to Write Data into Excel Sheet using Java?
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays