Skip to main content

How to convert a number from decimal to hexadecimal in Python

How to convert a number from decimal to hexadecimal in Python.

Here's a detailed step-by-step tutorial on how to convert a number from decimal to hexadecimal in Python:

Step 1: Understand the Decimal and Hexadecimal Number Systems

Before we dive into the conversion process, let's understand the two number systems involved:

  • Decimal Number System: This is the number system we commonly use in our day-to-day lives. It is a base-10 system, meaning it uses 10 digits (0-9) to represent numbers.
  • Hexadecimal Number System: This is a base-16 number system. In addition to the digits 0-9, it also includes six additional characters: A, B, C, D, E, and F. These extra characters represent the decimal values 10-15, respectively.

Step 2: Input the Decimal Number

To convert a decimal number to hexadecimal, you first need to input the decimal number you want to convert. Let's assume the decimal number is stored in a variable called decimal_num.

Step 3: Use the hex() Function

Python provides a built-in function called hex() that converts a decimal number to its hexadecimal representation. This function takes the decimal number as an argument and returns a string containing the hexadecimal representation.

Here's an example code snippet that demonstrates the usage of the hex() function:

decimal_num = 123
hex_num = hex(decimal_num)
print(hex_num)

Output:

0x7b

In this example, the decimal number 123 is converted to its hexadecimal representation, which is 0x7b.

Step 4: Remove the Prefix (Optional)

The hex() function in Python returns the hexadecimal representation with a prefix 0x. If you want to remove this prefix, you can do so using string manipulation techniques. Here's an example of how to remove the prefix:

decimal_num = 123
hex_num = hex(decimal_num)[2:] # Remove the first two characters
print(hex_num)

Output:

7b

In this example, the hex() function is called, and then the first two characters are removed from the returned string, resulting in the hexadecimal representation without the prefix.

Step 5: Convert Using Custom Algorithm (Alternative)

If you want to convert the decimal number to hexadecimal using a custom algorithm instead of relying on the hex() function, you can do so using the following steps:

  1. Initialize an empty string to store the hexadecimal representation.
  2. Divide the decimal number by 16 repeatedly until the quotient becomes zero. Keep track of the remainders in each division.
  3. Map each remainder to its corresponding hexadecimal digit. If the remainder is less than 10, use the remainder as it is. If the remainder is 10 or greater, use the corresponding letter A-F.
  4. Append each mapped digit to the beginning of the hexadecimal representation string.
  5. Once the quotient becomes zero, the hexadecimal representation is complete.

Here's an example code snippet that demonstrates this custom algorithm:

decimal_num = 123
hex_num = ''

while decimal_num > 0:
remainder = decimal_num % 16
if remainder < 10:
hex_num = str(remainder) + hex_num
else:
hex_num = chr(ord('A') + remainder - 10) + hex_num
decimal_num //= 16

print(hex_num)

Output:

7B

In this example, the decimal number 123 is converted to its hexadecimal representation using a custom algorithm. The resulting hexadecimal representation is 7B.

That's it! You now know how to convert a decimal number to hexadecimal in Python using both the hex() function and a custom algorithm.