36: Python Program Convert Hex String to Integer

Question: Write Python Program to Convert Hex String to Integer.


input_hex_string = input("Enter a hexadecimal number: ").strip()

    decimal_value = int(input_hex_string, 16)
    print(f"Decimal: {decimal_value}")
    print(f"Hexadecimal: {hex(decimal_value)[2:].upper()}")
except ValueError:
    print("Error! Invalid hexadecimal input.")


Enter a hexadecimal number: 2E
Decimal: 46
Hexadecimal: 2E

Enter a hexadecimal number: FFFFFF
Decimal: 16777215
Hexadecimal: FFFFFF

Enter a hexadecimal number: CAFEBABE
Decimal: 3405691582
Hexadecimal: CAFEBABE
Python Program Hex to Int Example

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap