Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Deciphering Challenge | Hexadecimal Numeral system
Numeral Systems 101

book
Deciphering Challenge

There is another commonly used numeral system called hexadecimal. If you learn web programming or something related to that, you should come across RGB Color Codes Chart that is implemented using hex(hexadecimal system) to help computers define different colors. Red -> #FF0000 White->#FFFFFF Yellow->#FFFF00 and the same representation for each color.

Hex is a representation of 4 bits. Computer professionals even consider reading the hexadecimal number easier than decimal one and binary. As I said previously it is a beautiful way of storing data not in a binary way, but to group it; hence, hexadecimal numerical system is implemented.

This one consists of 16 digits, 0->0 1->1 2->2 3->3 4->4 5->5 6->6 7->7 8->8 9->9. I suppose you start guessing the outcome of this sequence and involve 10 as the 10th number of this continuity, but you will be taken aback because 10->A 11->B 12->C 13->D 14->E 15->F But to convert it to decimal one, you should identify the dictionary, due to the reason that dictionaries are a superior way to hold information with a key. In the previous steps all of the keys conformed to numbers, but here due to the letters dictionary should be implemented.

Rule

I reckon that you are familiar with different algorithms so it seems to me that you can guess that here(in the hexadecimal numeral system) we are going to multiply each digit by 16 raised to the power of index. Obviously, we can not multiply the letter; therefore, we should find the math for it. For instance ABC10-> (A)10x16^4+(B)11x16^3+(C)12x16^2+1x16^1+0x16^0=655360+45056+3072+16+0=703504

# Implementing the dictionary
dictionary = {"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}
# Definig hexadecimal number
hexadecimal_number = "ABC10"
# The text should be realised here due to the reason that further the binary number will be changed
print("The number in hexadecimal numeral system is:", hexadecimal_number)
# Definig decimal number
decimal_number = 0
# Define variable for storing the power
power = 0
#the loop will iterate through the string hexadecimal_number
for digit in hexadecimal_number:
# Taking the very last character
digit = hexadecimal_number[-1]
# Multyplying the last digit to 16 raised the relevant power
result = dictionary[digit] * pow(16, power)
# Adding result to the decimal number
decimal_number = decimal_number+result
# Increasing power by 1
power = power + 1
# Removing the last sharacter of the string
hexadecimal_number = hexadecimal_number[0:-1]
# Printing the result
print("The number in decimal numeral system is:", decimal_number)
123456789101112131415161718192021222324
# Implementing the dictionary dictionary = {"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} # Definig hexadecimal number hexadecimal_number = "ABC10" # The text should be realised here due to the reason that further the binary number will be changed print("The number in hexadecimal numeral system is:", hexadecimal_number) # Definig decimal number decimal_number = 0 # Define variable for storing the power power = 0 #the loop will iterate through the string hexadecimal_number for digit in hexadecimal_number: # Taking the very last character digit = hexadecimal_number[-1] # Multyplying the last digit to 16 raised the relevant power result = dictionary[digit] * pow(16, power) # Adding result to the decimal number decimal_number = decimal_number+result # Increasing power by 1 power = power + 1 # Removing the last sharacter of the string hexadecimal_number = hexadecimal_number[0:-1] # Printing the result print("The number in decimal numeral system is:", decimal_number)
copy
Aufgabe

Swipe to start coding

I appreciate your desire to study, way to go! Write the code that will decode the number 'CAFE' from the hex numeral system to decimal. Follow this algorithm:

  1. Print the hexadecimal number.
  2. Assign 0 to the decimal_number variable.
  3. Define the variable power for storing the power and assign 0 to it.
  4. Define the loop which iterates through the hexadecimal_number string.
  5. Get the very last character of hexadecimal_number string.
  6. Raise 16 to the relevant power and multiply it by the digit.
  7. Increase power by 1.
  8. Remove the last character of the string hexadecimal_number.

Lösung

dictionary = {"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}
hexadecimal_number = "CAFE"
print("The number in hexadecimal numeral system is:", hexadecimal_number)
decimal_number = 0
power = 0
for digit in hexadecimal_number:
digit = hexadecimal_number[-1]
result = dictionary[digit] * pow(16, power)
decimal_number = decimal_number+result
power = power + 1
hexadecimal_number = hexadecimal_number[0:-1]
print("The number in decimal numeral system is:", decimal_number)

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 1
dictionary = {"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}
hexadecimal_number = "CAFE"
# Output the hexadecimal number
___("The number in hexadecimal numeral system is: ", ___)
# Assign 0 to the decimal_number variable
decimal_number = ___
# Define variable power for storing the power and assign 0 to it
___ = ___
# Define the loop which iterates through the hexadecimal_number string
___ digit in ___:
# Get the very last character
digit = hexadecimal_number[___]
# Raise 16 to the relevant power and multiply it by the digit
result = dictionary[___] * pow(___)
decimal_number = decimal_number + result
# Increase power by 1
power = ___ ___
# Remove the last character of the string hexadecimal_number
hexadecimal_number = ___
print("The number in decimal numeral system is: ", decimal_number)

Fragen Sie AI

expand
ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

some-alt