Back to Practice Dashboard
Python BasicsEasy
Hexadecimal to Decimal conversion
Learn how to solve the 'Hexadecimal to Decimal conversion' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Easy
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.
Constraints
- •1 <= len(hex_str) <= 8
- •hex_str contains only valid hex characters (0-9, a-f, A-F)
Examples
Example 1
Input
hex_to_decimal('1A')Output
26
Explanation
1×16¹ + A(10)×16⁰ = 16 + 10 = 26.
Example 2
Input
hex_to_decimal('FF')Output
255
Explanation
F(15)×16¹ + F(15)×16⁰ = 240 + 15 = 255.
Example 3
Input
hex_to_decimal('2B')Output
43
Explanation
2×16¹ + B(11)×16⁰ = 32 + 11 = 43.
Need a Hint?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.