转换 Numbers Between Radices
转换 integers between binary, octal, decimal, hexadecimal, and arbitrary radices.
问题
你有 数字在 one 进制 (e.g., hex) 和 need 它在 另一个 (e.g., 二进制或 十进制) 为不同 tool 或上下文.
前置条件
- Understanding 的 positional numeral systems
- 基础 arithmetic
步骤
-
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.
输入source: hex 1A | target: binary输出radices: 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.
输入1A (hex)输出26 (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).
输入11010 (binary)输出1A (hex) ✓
预期结果
相同 numeric 值 represented 在目标基数, 具有所有 digits 有效为 该进制.
常见错误
- 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.
排障指引
- 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.
安全限制
- Pure arithmetic conversion with no security implications.