Kodeclik Logo

Our Programs

Courses

Learn More

Schedule

Kodeclik Blog

How to Add Hexadecimal Numbers in Python

Recall that Hexadecimal is a base-16 number system that uses sixteen distinct symbols: the numbers 0-9 and the letters A-F (or a-f), where A represents 10, B represents 11, C represents 12, D represents 13, E represents 14, and F represents 15.

How to add two hexadecimal numbers

The easiest way to add two hexadecimal numbers is to line them up just like you would in regular addition. Then add digits from right to left. If the sum is ≥ 16, carry over 1 to the next column. Convert sums > 9 to hex digits when finalizing the sum.

Let us add the numbers 8C and A7.

One way to do it is:

How to add two hexadecimal numbers in Python

Starting from the rightmost column, we add C (which is 12 in decimal) and 7, giving us 19 in decimal. Since 19 is greater than 16, we write down 3 (19 - 16 = 3) and carry 1 to the next column. In the next column, we add 8 + A (which is 10 in decimal) plus our carried 1, giving us 8 + 10 + 1 = 19. When we convert 19 to hexadecimal, it becomes 13. Therefore, our final answer is 133 in hexadecimal, where the leftmost 1 comes from the carried value, and the rightmost 3 comes from our first calculation.

Another way to convince yourself this is true is to do these calculations by first converting the original numbers to decimal, adding them, and then converting the result back to hexadecimal. Here’s how that would work:

How to add two hexadecimal numbers in Python

To convert 8C to decimal, we multiply each digit by the corresponding power of 16: 8 is in the sixteens place, so that's 8 × 16 = 128, and C (which equals 12) is in the ones place, giving us 12 × 1 = 12. Adding these together, 8C equals 140 in decimal. For A7, we follow the same process: A (which equals 10) is in the sixteens place, so that's 10 × 16 = 160, and 7 is in the ones place, giving us 7 × 1 = 7. Adding these together, A7 equals 167 in decimal.

Now we can perform simple decimal addition: 140 + 167 = 307. Converting this result back to hexadecimal, we divide by 16 repeatedly and keep track of the remainders: 307 ÷ 16 = 19 remainder 3, then 19 ÷ 16 = 1 remainder 3, and finally 1 remains. Reading the digits from last to first gives us 133 in hexadecimal. This confirms our previous manual calculation that 8C + A7 = 133 in hexadecimal.

Let us now see how to add hexadecimal numbers in Python! Before that we need to understand how hexadecimal numbers are represented in Python.

Representing hexadecimal numbers in Python

Hexadecimal numbers in Python start with the prefix '0x' or '0X':

# Define hex numbers
hex1 = 0xFF    # 255 in decimal
hex2 = 0x1A    # 26 in decimal
hex3 = 0xabc   # lowercase is fine
hex4 = 0XDEF   # uppercase is also fine

# Print to see different representations
print(hex1)         # prints: 255
print(hex2)         # prints: 26
print(hex(hex1))    # prints: 0xff
print(hex(hex2))    # prints: 0x1a

Note that when we print hex numbers in Python, their representation varies based on how we print them. The first two print statements (print(hex1) and print(hex2)) output the decimal equivalents of the hexadecimal numbers because Python automatically converts numbers to decimal when displaying them directly - so 0xFF becomes 255 and 0x1A becomes 26. The next two print statements use the hex() function, which explicitly converts the numbers back to their hexadecimal representation, complete with the '0x' prefix - so hex(hex1) shows 0xff and hex(hex2) shows 0x1a.

Notice that when using hex(), Python outputs the hexadecimal digits in lowercase, regardless of how they were originally defined in the code.

To have control over lowercase or uppercase printing, Python also offers different ways to format hex output:

number = 255
# Different format options
print(f'{number:x}')    # prints: ff
print(f'{number:X}')    # prints: FF
print(f'{number:#x}')   # prints: 0xff
print(f'{number:#X}')   # prints: 0xFF

Now let us see how to add two hexadecimal numbers in Python

Method 1: Direct addition of two hexadecimal numbers in Python

When using hex literals directly in code, you can simply use the + operator:

result = 0xf + 0x1  # adds hex numbers directly
print(hex(result))  # prints: 0x10

Here we are adding “F” meaning 15 and 1, giving 16 which in hex is denoted as “10”.

Method 2: Adding Hex Strings in Python

If you have hexadecimal numbers as strings, convert them to integers first using int() with base 16:

hex1 = '0xf'
hex2 = '0x1'
result = int(hex1, 16) + int(hex2, 16)
final_hex = hex(result)  # converts result back to hex string
print(final_hex)

Let's break down this code line by line. The first two lines create string variables hex1 and hex2 containing hexadecimal numbers as text ('0xf' and '0x1'). Since these are strings, we can't add them directly - we need to convert them to integers first.

The int(hex1, 16) function converts the string '0xf' to the decimal number 15, and int(hex2, 16) converts '0x1' to the decimal number 1 - the second parameter 16 tells Python to interpret these strings as hexadecimal numbers. These decimal numbers are then added together (15 + 1 = 16). Finally, hex(result) converts the decimal sum 16 back to a hexadecimal string '0x10', which is then printed.

So when you run this code, it will output:

0x10

If you need to print the result without 0x, you can modify the program like so:

hex1 = '0xf'
hex2 = '0x1'
result = int(hex1, 16) + int(hex2, 16)
final_hex = hex(result)  # converts result back to hex string
print(final_hex[2:])

This will output:

10

as expected.

Adding hex numbers from user input

Here's how to handle hex number addition from user input:

# Get hex numbers from user
first = input("Enter first hex number: ")
second = input("Enter second hex number: ")

# Convert and add
sum_result = int(first, 16) + int(second, 16)
hex_result = hex(sum_result)
print(hex_result)

This code creates an interactive program for adding hexadecimal numbers. The first two lines prompt the user to enter two hexadecimal numbers using input() - these numbers can be entered with or without the '0x' prefix. The program then converts both input strings to decimal integers using int(first, 16) and int(second, 16), where the base 16 parameter tells Python to interpret the input as hexadecimal. These decimal numbers are added together in sum_result. The hex() function then converts this decimal sum back to a hexadecimal string with the '0x' prefix, which is stored in hex_result and printed. For example, if a user enters 'FF' and '1', the program will output '0x100' because FF (255 in decimal) plus 1 equals 256 in decimal, which is 100 in hexadecimal.

Let us try it out with our own numbers! Here is a sample run:

Enter first hex number: 
E
Enter second hex number: 
E
0x1c

You can convince yourself that this is true because E is 14. 14+14 is 28, which is represented as “1C” in hex (16+12).

Enjoy this blogpost? Want to learn Python with us? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2024. All rights reserved.