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 Decimal to Hexadecimal in Python? Program and Examples
Numbers are the foundation for representing and manipulating data in the world of programming languages. While we are familiar with the decimal system, which is based on the number 10, computer systems uses various number systems to optimize data storage and computation. One such system is the hexadecimal number system.
The hexadecimal system is a base-16 number system, consisting of 16 distinct digits: 0-9 and A-F. Each digit represents a unique value in powers of 16. Understanding how to convert decimal to hexadecimal in Python is an essential skill for programmers, as it facilitates efficient data representation and analysis in diverse programming scenarios.
Understanding decimal-to-hexadecimal conversion brings you valuable skills in various programming domains, including low-level programming, data encoding and decoding, network protocols, and cryptographic algorithms.
Hexadecimal representation is particularly useful when working with memory addresses, color codes, binary data, and cryptographic keys.
By learning how to convert decimal to hexadecimal in Python, you gain a powerful tool to tackle complex programming challenges and gain a deeper understanding of the inner workings of computer systems.
Python Program to Convert Decimal to Hexadecimal
Code
def decimal_to_hexadecimal(decimal):
hexadecimal = ""
hex_digits = "0123456789ABCDEF"
while decimal > 0:
remainder = decimal % 16
hexadecimal = hex_digits[remainder] + hexadecimal
decimal = decimal // 16
return hexadecimal
if __name__ == "__main__":
decimal = int(input("Enter a decimal number: "))
hexadecimal = decimal_to_hexadecimal(decimal)
print("Hexadecimal: " + hexadecimal)
Output
Enter a decimal number: 532
Hexadecimal: 214
Explanation
LCM in Python Using for loop
The decimal_to_hexadecimal() function takes a decimal number as input and returns its hexadecimal representation as a string.
Inside the function, we initialize an empty string hexadecimal to store the hexadecimal digits.
We define the string hex_digits which contains the 16 possible hexadecimal digits: "0123456789ABCDEF".
Using a while loop, we repeatedly perform the following steps until the decimal number becomes zero:
-
We compute the remainder when the decimal number is divided by 16 using the modulus operator %.
-
We retrieve the corresponding hexadecimal digit from the hex_digits string using the remainder as an index.
-
We concatenate the hexadecimal digit with the left side of the hexadecimal string.
-
We update the decimal number by performing integer division by 16 (decimal // 16).
After the while loop ends, we have obtained the hexadecimal representation of the decimal number.
Finally, we return the resulting hexadecimal string.
In the main program, we prompt the user to enter a decimal number using the input() function, and we convert it to an integer using int().
We call the decimal_to_hexadecimal() function with the decimal number as an argument and store the result in the hexadecimal variable.
Finally, we print the hexadecimal representation of the decimal number using the print() function, along with an appropriate label.
By using this program, you can convert any decimal number to its hexadecimal
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