# Program to Convert Octal to Hexadecimal

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

**Examples:**

Input:47Output:27Explanation: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 = 27Input:235Output: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.

**For example** if the given octal number is 5123:

Digit | Power | Multiplication |
---|---|---|

5 | 512 | 2560 |

1 | 64 | 64 |

2 | 8 | 16 |

3 | 1 | 3 |

Then the decimal number (2560 + 64 + 16 + 3) is: 2643

Division | Quotient | Remainder |
---|---|---|

2643/16 | 165 | 3 |

165/16 | 10 | 5 |

10/16 | 0 | 10 (a) |

Finally, the hexadecimal number is: a53

Below is the implementation of the above approach:

## Java

`// 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); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to convert octal ` `# to hexadecimal ` ` ` `# Taking 5123 as an example of ` `# octal number ` `octnum ` `=` `"5123"` ` ` `# Convert octal to decimal ` `decnum ` `=` `int` `(octnum, ` `8` `) ` ` ` `# Convert decimal to hexadecimal ` `hexadecimal ` `=` `hex` `(decnum).replace(` `"0x"` `, "") ` ` ` `# Printing the hexadecimal value ` `print` `(` `"Equivalent Hexadecimal Value ="` `, hexadecimal) ` ` ` `# This code is contributed by virusbuddah_ ` |

*chevron_right*

*filter_none*

**Output:**

Equivalent Hexadecimal Value = a53

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Program to Convert Hexadecimal Number to Binary
- Program to Convert Octal Number to Binary Number
- Convert a string to hexadecimal ASCII values
- Program for decimal to hexadecimal conversion
- Convert a binary number to hexadecimal number
- Program to convert a Map to a Stream in Java
- Program to Convert Radian to Degree
- Program to Convert Set to List in Java
- Program to convert Degree to Radian
- Program to convert Array to Set in Java
- Program to convert a given number to words
- Program to convert a given number to words | Set 2
- Program to convert Number in characters
- Program to Convert Stream to an Array in Java
- Program to convert first character uppercase in a sentence
- Program to Convert a Vector to List in Java
- Program to convert centimeter into meter and kilometer
- Python Program to convert Kilometers to Miles
- Program to convert IntStream to String in Java
- Program to Convert List to Stream in Java

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.