Hexadecimal to Decimal conversion
Detailed guide and Python implementation for the 'Hexadecimal to Decimal conversion' problem.
Problem Statement
Write a function hex_to_decimal(hex_str) that takes a string representing a hexadecimal number (base-16, digits 0-9 and A-F, case-insensitive) and returns its decimal (base-10) integer equivalent.
In hexadecimal, A=10, B=11, C=12, D=13, E=14, F=15. Each digit represents a power of 16.
- •1 <= len(hex_str) <= 8
- •hex_str contains only valid hex characters (0-9, a-f, A-F)
Examples
hex_to_decimal('1A')26
1×16¹ + A(10)×16⁰ = 16 + 10 = 26.
hex_to_decimal('FF')255
F(15)×16¹ + F(15)×16⁰ = 240 + 15 = 255.
hex_to_decimal('2B')43
2×16¹ + B(11)×16⁰ = 32 + 11 = 43.
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Generators
Learn how to use Python generators and yield statements to process huge datasets with minimal memory footprints. Master generator expressions.
How to Convert String to Int in Python
Learn how to convert a string to an integer in Python using the int() function. Handle errors safely and convert numbers from binary, octal, or hex.
Python Type Conversions
Learn implicit and explicit type conversions in Python. Convert between strings, integers, floats, lists, sets, and dictionaries.
Python Decorators vs Decorator Design Pattern: The Key Differences
Compare Python decorators and the classic decorator design pattern. Understand the differences between definition-time function wrapping and runtime dynamic object composition with runnable code.