Python BasicsEasy

Hexadecimal to Decimal conversion

Detailed guide and Python implementation for the 'Hexadecimal to Decimal conversion' problem.

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?
Consider using Numbers-specific data structures like sets or heaps.
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.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.