Jump to content

Converting hex to dec (python3)

lewdicrous
Go to solution Solved by lewdicrous,

Thank you @tikker for showing me where I went wrong, it also helped me troubleshoot another question :D

Spoiler

Solution: removing toDecimal() from displayDecimal()

Thank you @panther420 for providing a clean way of converting the hexadecimal symbols to integers without using a lot of if-statements :D

Spoiler

hex_to_dec = { '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 }

def toDecimal(h3,h2,h1,h0):
    dec3 = (hex_to_dec[h3] * 4096)
    dec2 = (hex_to_dec[h2] * 256)
    dec1 = (hex_to_dec[h1] * 16)
    dec0 = (hex_to_dec[h0] * 1)
    return dec3, dec2, dec1, dec0

 

 

Spoiler

def main():
    H = input("Please enter a 4-digit positive integer H: ")
    h3,h2,h1,h0 = readHexadecimal(H) # calls the function readHexadecimal
    print("The hexadecimal number is %s" %H)
    dec3,dec2,dec1,dec0 = toDecimal(h3,h2,h1,h0) # calls the function toDecimal
    displayDecimal(dec3,dec2,dec1,dec0) # calls the function displayDecimal

## read the input
# @param the hexadecimal number, H
# @return the digit separated into 4 digits (H=h3,h2,h1,h0)
def readHexadecimal(H):
    h3 = H[0]
    h2 = H[1]
    h1 = H[2]
    h0 = H[3]
    print(h3,h2,h1,h0)
    return h3, h2, h1, h0

## converts the hexadecimal to decimal using positional notation
# @param the 4 digits from readHexadecimal()
# @return the 4 digits converted from hex to dec
def toDecimal(h3,h2,h1,h0):
    dec3 = (int(h3) * 4096)
    dec2 = (int(h2) * 256)
    dec1 = (int(h1) * 16)
    dec0 = (int(h0) * 1)
    return dec3, dec2, dec1, dec0

## print the result from toDecimal()
# @param the 4 converted digits from toDecimal()
def displayDecimal(dec3,dec2,dec1,dec0):
    dec3,dec2,dec1,dec0 = toDecimal(dec3,dec2,dec1,dec0)
    print ("The decimal number is %d" %(dec3+dec2+dec1+dec0)) # adding the 4 digits to complete the conversion
   
main()

 

Hello, I'm trying to convert a number from base 16 (hex) to base 10 (dec) using positional notations but i keep getting the wrong answer, for example, (1111)16 = (4369)10 but when I try it using my program I get 16843009

Where am I going wrong?

I still did not write the necessary codes to convert letters to numbers {"A":10, "B":11...} if you know how to neatly incorporate that into the code then that would be much appreciated.

 

Note:

I know that you can use int() to convert into bases, the question asks me to use positional notations instead.

The integer, H, only consists of hexadecimal digits.

The functions:

  • readHexadecimal() reads the integer H
  • toDecimal() converts the hexadecimal to decimal
  • displayDecimal() prints the decimal value

 

Thank you

Link to comment
Share on other sites

Link to post
Share on other sites

31 minutes ago, lewdicrous said:
  Reveal hidden contents


def main():
    H = input("Please enter a 4-digit positive integer H: ")
    h3,h2,h1,h0 = readHexadecimal(H) # calls the function readHexadecimal
    print("The hexadecimal number is %s" %H)
    dec3,dec2,dec1,dec0 = toDecimal(h3,h2,h1,h0) # calls the function toDecimal
    displayDecimal(dec3,dec2,dec1,dec0) # calls the function displayDecimal

## read the input
# @param the hexadecimal number, H
# @return the digit separated into 4 digits (H=h3,h2,h1,h0)
def readHexadecimal(H):
    h3 = H[0]
    h2 = H[1]
    h1 = H[2]
    h0 = H[3]
    print(h3,h2,h1,h0)
    return h3, h2, h1, h0

## converts the hexadecimal to decimal using positional notation
# @param the 4 digits from readHexadecimal()
# @return the 4 digits converted from hex to dec
def toDecimal(h3,h2,h1,h0):
    dec3 = (int(h3) * 4096)
    dec2 = (int(h2) * 256)
    dec1 = (int(h1) * 16)
    dec0 = (int(h0) * 1)
    return dec3, dec2, dec1, dec0

## print the result from toDecimal()
# @param the 4 converted digits from toDecimal()
def displayDecimal(dec3,dec2,dec1,dec0):
    dec3,dec2,dec1,dec0 = toDecimal(dec3,dec2,dec1,dec0)
    print ("The decimal number is %d" %(dec3+dec2+dec1+dec0)) # adding the 4 digits to complete the conversion
   
main()

 

Hello, I'm trying to convert a number from base 16 (hex) to base 10 (dec) using positional notations but i keep getting the wrong answer, for example, (1111)16 = (4369)10 but when I try it using my program I get 16843009

Where am I going wrong?

I still did not write the necessary codes to convert letters to numbers {"A":10, "B":11...} if you know how to neatly incorporate that into the code then that would be much appreciated.

 

Note:

I know that you can use int() to convert into bases, the question asks me to use positional notations instead.

The integer, H, only consists of hexadecimal digits.

The functions:

  • readHexadecimal() reads the integer H
  • toDecimal() converts the hexadecimal to decimal
  • displayDecimal() prints the decimal value

 

Thank you

Quick search on stackoverflow revealed me this: 

image.png.d6b49b39a2adff6f2006991bef5037fe.png

Proof:

image.png.28041364b7e54ddf49a6f04e7b302306.png

EDIT: Nevermind, I just reread your post and saw that you cant use int. Give me one sec.

Spoiler

My main desktop, "Rufus":

Spoiler

PC Specs:

CPU: AMD Ryzen 5 1600

CPU Cooler: Cooler Master MasterLiquid Lite 120

RAM: 2x8gb Corsair Vengence DDR4 Red LED @ 3066mt/s

Motherboard: MSI B350 Gaming Pro Carbon

GPU: XFX RX 580 GTR XXX White 

Storage: Mushkin ECO3 256GB SATA3 SSD + Some hitachi thing

PSU: Seasonic Focus Plus Gold 650W

Case: Corsair Crystal 460X

OS: Windows 10 x64 Pro Version 1607

Retro machine:

Spoiler

PC Specs:

CPU: Intel Core 2 Quad Q9550

CPU Cooler: Stock heatsink

RAM: GSkill 4gb DDR2 1066mt/s

Motherboard: Asus P5n-e SLI

GPU: 8800 GTS 640mb, I swap between that and my 8800 GTS 512mb

Storage: Seagate 320gb right from 2006

PSU: Ultra 600W 

Case: Deepcool Tesseract SW

OS: Windows XP SP3 32-bit, Linux Mint 18.2 Cinnamon 64-bit, Manjaro Deepin x64 (sorta)

Mac Pro Early 2008: Dual Xeon X5482s w/ 32GB RAM & HD 5770 running macOS High Sierra

More PC's

 

Link to comment
Share on other sites

Link to post
Share on other sites

Your mistake is calling toDecimal twice. You call it both in the main function and in the displayDecimal function.

The actual code works fine.

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, tikker said:

Your mistake is calling toDecimal twice. You call it both in the main function and in the displayDecimal function.

The actual code works fine.

didn't actually know that'll fix it.. i stayed up till 8am just trying to figure it out :')

Thank you!

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, lewdicrous said:

didn't actually know that'll fix it.. i stayed up till 8am just trying to figure it out :')

Thank you!

I found a solution to the hexadecimal character mapping problem, but I'm not sure if it's against the rules for the problem you're solving. It's just using int(hX, 16) in the toDecimal method to map the characters:

def toDecimal(h3,h2,h1,h0):
    dec3 = (int(h3, 16) * 4096)
    dec2 = (int(h2, 16) * 256)
    dec1 = (int(h1, 16) * 16)
    dec0 = (int(h0, 16) * 1)
    return dec3, dec2, dec1, dec0

Otherwise a dictionary like you proposed would work fine: 

hex_to_dec = { '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 }

def toDecimal(h3,h2,h1,h0):
    dec3 = (hex_to_dec[h3] * 4096)
    dec2 = (hex_to_dec[h2] * 256)
    dec1 = (hex_to_dec[h1] * 16)
    dec0 = (hex_to_dec[h0] * 1)
    return dec3, dec2, dec1, dec0

 

Spoiler

My main desktop, "Rufus":

Spoiler

PC Specs:

CPU: AMD Ryzen 5 1600

CPU Cooler: Cooler Master MasterLiquid Lite 120

RAM: 2x8gb Corsair Vengence DDR4 Red LED @ 3066mt/s

Motherboard: MSI B350 Gaming Pro Carbon

GPU: XFX RX 580 GTR XXX White 

Storage: Mushkin ECO3 256GB SATA3 SSD + Some hitachi thing

PSU: Seasonic Focus Plus Gold 650W

Case: Corsair Crystal 460X

OS: Windows 10 x64 Pro Version 1607

Retro machine:

Spoiler

PC Specs:

CPU: Intel Core 2 Quad Q9550

CPU Cooler: Stock heatsink

RAM: GSkill 4gb DDR2 1066mt/s

Motherboard: Asus P5n-e SLI

GPU: 8800 GTS 640mb, I swap between that and my 8800 GTS 512mb

Storage: Seagate 320gb right from 2006

PSU: Ultra 600W 

Case: Deepcool Tesseract SW

OS: Windows XP SP3 32-bit, Linux Mint 18.2 Cinnamon 64-bit, Manjaro Deepin x64 (sorta)

Mac Pro Early 2008: Dual Xeon X5482s w/ 32GB RAM & HD 5770 running macOS High Sierra

More PC's

 

Link to comment
Share on other sites

Link to post
Share on other sites

Thank you @tikker for showing me where I went wrong, it also helped me troubleshoot another question :D

Spoiler

Solution: removing toDecimal() from displayDecimal()

Thank you @panther420 for providing a clean way of converting the hexadecimal symbols to integers without using a lot of if-statements :D

Spoiler

hex_to_dec = { '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 }

def toDecimal(h3,h2,h1,h0):
    dec3 = (hex_to_dec[h3] * 4096)
    dec2 = (hex_to_dec[h2] * 256)
    dec1 = (hex_to_dec[h1] * 16)
    dec0 = (hex_to_dec[h0] * 1)
    return dec3, dec2, dec1, dec0

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

21 minutes ago, lewdicrous said:

Thank you @tikker for showing me where I went wrong, it also helped me troubleshoot another question :D

  Reveal hidden contents

Solution: removing toDecimal() from displayDecimal()

Thank you @panther420 for providing a clean way of converting the hexadecimal symbols to integers without using a lot of if-statements :D

  Hide contents


hex_to_dec = { '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 }

def toDecimal(h3,h2,h1,h0):
    dec3 = (hex_to_dec[h3] * 4096)
    dec2 = (hex_to_dec[h2] * 256)
    dec1 = (hex_to_dec[h1] * 16)
    dec0 = (hex_to_dec[h0] * 1)
    return dec3, dec2, dec1, dec0

 

 

And now for the next challenge: allow inputs of arbitrary length :D

 

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
Share on other sites

Link to post
Share on other sites

Python libraries are seriously making things like these easy. 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, wasab said:

Python libraries are seriously making things like these easy. 

I know, but the question asks you to use a certain method, positional notation, as I've mentioned at the end of the post

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×