Convert Numbers Between Radices
Convert integers between binary, octal, decimal, hexadecimal, and arbitrary radices.
Problem
You have a number in one base (e.g., hex) and need it in another (e.g., binary or decimal) for a different tool or context.
Prerequisites
- Understanding of positional numeral systems
- Basic arithmetic
Steps
-
1. Identify the source and target radix
Determine the base of your input number and the base you need as output. Common radices are 2 (binary), 8 (octal), 10 (decimal), and 16 (hex). The source value must be valid in its claimed base.
Inputsource: hex 1A | target: binaryOutputradices: 16 → 2 -
2. Convert through decimal or directly
The simplest method is to first parse the input as a decimal integer, then format that integer in the target base. For direct hex-to-binary, each hex digit maps to 4 bits without intermediate conversion.
Input1A (hex)Output26 (decimal) → 11010 (binary) -
3. Verify the result
Convert the output back to the original base and confirm it matches the input. Check that no digits in the output exceed the target radix (e.g., no digit 8 in octal).
Input11010 (binary)Output1A (hex) ✓
Expected result
The same numeric value represented in the target radix, with all digits valid for that base.
Common errors
- Using digits invalid for the source base
- Hex allows A-F, but binary only allows 0-1. Validate the input before converting.
- Losing leading zeros
- Leading zeros are significant in fixed-width contexts like byte representations. Pad the output to the required width after conversion.
Troubleshooting
- If the result seems too large or small, double-check the source radix. A hex value read as decimal will be wrong.
- For fractional numbers, note that this tutorial covers integers only; fractional conversion requires separate handling.
Security limits
- Pure arithmetic conversion with no security implications.
Related tools
Related tasks
Ready to run it?