Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
How to Convert Hexadecimal to Octal in Python? Program With Examples
In computer programming, understanding and manipulating different number systems is crucial for effectively working with data. While decimal numbers (base-10) are commonly used in everyday life, computer systems often employ hexadecimal (base-16) and octal (base-8) numbers for specific purposes.
Hexadecimal to octal conversion in Python is a valuable skill that allows programmers to interpret and work with data in different representations.
Hexadecimal numbers use a combination of digits from 0 to 9 and letters from A to F to represent values from 0 to 15. Octal numbers, on the other hand, use digits from 0 to 7 to represent values from 0 to 7.
Understanding how to convert hexadecimal to octal in Python programming is essential for programmers, as it enables efficient data representation and manipulation in various programming scenarios.
In this post, we will learn the process of converting hexadecimal numbers to octal in Python programming language. We will provide a step-by-step guide, complete with examples, to help you grasp the concept and apply it effectively in your Python projects. You can practice the given Python programs on your own with an online Python compiler.
Python Program to Convert Hexadecimal to Octal
Code
def hexadecimal_to_octal(hexadecimal):
decimal = 0
power = 0
while hexadecimal > 0:
last_digit = hexadecimal % 10
decimal += last_digit * (16 ** power)
hexadecimal //= 10
power += 1
octal = 0
power = 0
while decimal > 0:
last_digit = decimal % 8
octal += last_digit * (10 ** power)
decimal //= 8
power += 1
return octal
hexadecimal_number = input("Enter a hexadecimal number: ")
decimal_number = int(hexadecimal_number, 16)
octal_number = hexadecimal_to_octal(decimal_number)
print("Octal equivalent:", octal_number)
Output
Enter a hexadecimal number: 755
Octal equivalent: 14167
Explanation
In this program, we define a function called hexadecimal_to_octal that takes a hexadecimal number as input and returns its octal equivalent.
First, we convert the hexadecimal number to decimal by using the int function and specifying the base as 16.
We then use a while loop to convert the decimal number to octal. Next, we extract the last digit using the modulus operator %, multiply it by the corresponding power of 8, and add it to the octal number.
We update the decimal number by integer division // to remove the last digit, and increment the power by 1. This process continues until the decimal number becomes 0.
Finally, we prompt the user to enter a hexadecimal number, convert it to an integer using int and base 16, call the hexadecimal_to_octal function, and print the octal equivalent.
Practice More Python Programs:
- Hello World Program in Python
- Python Program to Add Two Numbers
- Generate Random Number in Python
- Leap Year Program in Python
- Even Odd Program in Python
- Python Program to Find Largest of 3 Numbers
- Prime Number Program in Python
- Python Factorial Program
- Python Program to Find Area of Triangle
- Solve Quadratic Equation in Python
- Swap Two Numbers in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Check Armstrong Numbers in Python
- Print Multiplication Table in Python
- Python Program for Fibonacci Series
- Convert Decimal to Binary, Octal and Hexadecimal in Python
- Find LCM of Two Numbers in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program