NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

# List of characters that represent hexadecimal digits greater than 9 (A=10, B=11, ..., F=15)
hexx_list = ["A", "B", "C", "D", "E", "F"]

# Hexadecimal string that we want to convert to a decimal number
hexx = "AF"

# Initialize a variable to store the final decimal value
den = 0

# Loop through each character in the hexadecimal string `hexx`, starting from the right (least significant digit)
for i in range(len(hexx)):
# If the current character is a letter (i.e., A, B, C, D, E, F), we need to convert it to its decimal equivalent
if hexx[-1 - i] in hexx_list: # Access characters from right to left using negative indexing
# Find the index of the current character in `hexx_list` and add 10 to get its decimal equivalent
# A = 10, B = 11, ..., F = 15
hex_p = hexx_list.index(hexx[-1 - i]) + 10
print(hex_p) # Optional: print the decimal equivalent of the hexadecimal character (for debugging)
else:
# If the character is not a letter (A-F), it's a regular digit (0-9), so convert it directly to an integer
hex_p = int(hexx[-1 - i])

# Calculate the place value of the current hexadecimal digit (16^i) and multiply it by the decimal value (hex_p)
place = hex_p * (16 ** i)

# Add the place value to the final decimal result
den += place

# Output the final decimal value of the hexadecimal string
print(den)





Step-by-Step Explanation:
Hexadecimal Characters: The list hexx_list holds the characters A to F, which correspond to the numbers 10 to 15 in hexadecimal. In hexadecimal:

A = 10, B = 11, C = 12, D = 13, E = 14, F = 15.
Input Hexadecimal String: The string hexx = "AF" is the hexadecimal number that we want to convert to a decimal number.

Initialize Decimal Value: The variable den = 0 will be used to accumulate the final decimal value as we convert each hexadecimal digit.

Loop Over Hexadecimal String: The for loop goes through each character in the hexadecimal string hexx from right to left:

hexx[-1 - i] is used for accessing characters from the rightmost (least significant) to the leftmost (most significant).
For example, if hexx = "AF", on the first iteration, hexx[-1] gives "F", and on the second iteration, hexx[-2] gives "A".
Check for Hexadecimal Letters: The if condition checks whether the current character is one of the letters A to F:

If it is a letter, the index() method is used to find its position in hexx_list and convert it to its corresponding decimal value by adding 10 (since A=10, B=11, ..., F=15).
If the character is a regular number (0-9), it is directly converted to an integer using int().
Calculate Place Value: The value of the current hexadecimal digit is then multiplied by 16**i, where i is the position of the character from the right (0 for the rightmost digit, 1 for the second from the right, etc.). This is how place values work in hexadecimal:

For example, in AF:
F is in the "ones" place, so its value is multiplied by 16^0.
A is in the "sixteens" place, so its value is multiplied by 16^1.
Accumulate the Decimal Value: The place value of the current digit is added to the running total den.

Output the Final Result: After all digits are processed, the final decimal value is printed.

Example Walkthrough for "AF":
AF in hexadecimal is A*16 + F*1.
A corresponds to 10, so 10 * 16 = 160.
F corresponds to 15, so 15 * 1 = 15.
Total: 160 + 15 = 175.
The program will output 175 as the decimal equivalent of the hexadecimal string "AF".
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.