Teaching Kids Programming – Hexadecimal Numbers Conversion


Teaching Kids Programming: Videos on Data Structures and Algorithms

Algorithm to Convert Decimal Numbers to Hexadecimal

Hexadecimal Numbers (base 16 because hex is 6 and decimal is 10) use ‘0’ to ‘9’ and ‘A’ to ‘F’ (lowercases are also ok). Similar to Converting Integers (Base 10) to Binary, we can use a mapping table and continuously divide 16 and select the remainder, concatenate it in the reverse order:

1
2
3
4
5
6
7
8
9
10
def dec2hex(s):
    mapping = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"]
    ans = ""
    while s > 0:
        ans = mapping[s % 16] + ans
        s //= 16
    if ans == "": # when given 0, we should return 0
        return "0"
    else:
        return ans
def dec2hex(s):
    mapping = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"]
    ans = ""
    while s > 0:
        ans = mapping[s % 16] + ans
        s //= 16
    if ans == "": # when given 0, we should return 0
        return "0"
    else:
        return ans

When input number is zero, we should return “0” instead of empty string (hexadecimal).

Algorithm to Convert the Hexadecimal Numbers to Decimal (Base 10)

We can sum up each digit’s power. This is the similar process as converting the binary numbers to base 10. Shifting one position to the left in base 16 means multiple the value by 16.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def hex2dec(s):
    ans = 0
    mapping = {
        "0": 0,
        "1": 1,
        "2": 2,
        "3": 3,
        "4": 4,
        "5": 5,
        "6": 6,
        "7": 7,
        "8": 8,
        "9": 9,
        "A": 10,
        "B": 11,
        "C": 12,
        "D": 13,
        "E": 14,
        "F": 15,        
    }
    for i in s:
        ans = ans * 16 + mapping[i.upper()]
    return ans
def hex2dec(s):
    ans = 0
    mapping = {
        "0": 0,
        "1": 1,
        "2": 2,
        "3": 3,
        "4": 4,
        "5": 5,
        "6": 6,
        "7": 7,
        "8": 8,
        "9": 9,
        "A": 10,
        "B": 11,
        "C": 12,
        "D": 13,
        "E": 14,
        "F": 15,        
    }
    for i in s:
        ans = ans * 16 + mapping[i.upper()]
    return ans

We use the upper() method to convert a character to its uppercase. Non-letters won’t change. Last, we can test if the above two methods are working by the following code snippet.

1
2
3
4
5
6
7
for i in range(255):
    hex = dec2hex(i)
    dec = hex2dec(hex)
    if i != dec:
        print(dec, "=", hex)
        print("Test Failed: " + str(i) + " != " + str(dec))
print("Done.")
for i in range(255):
    hex = dec2hex(i)
    dec = hex2dec(hex)
    if i != dec:
        print(dec, "=", hex)
        print("Test Failed: " + str(i) + " != " + str(dec))
print("Done.")

–EOF (The Ultimate Computing & Technology Blog)

GD Star Rating
loading...
346 words
Last Post: Recursive Algorithm to Convert a List to Binary Search Tree
Next Post: Algorithm to Remove Duplicates in Linked List using Hash Set

The Permanent URL is: Teaching Kids Programming – Hexadecimal Numbers Conversion

Leave a Reply