Convertir Numbers Between Radices
Convertir integers between binary, octal, decimal, hexadecimal, and arbitrary radices.
问题
You tener un número en one base (e.g., hex) y necesitar it en another (e.g., binary o decimal) para un diferente herramienta o context.
Requisitos previos
- Understanding of positional numeral systems
- Basic arithmetic
Pasos
-
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.
entradasource: 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.
entrada1A (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).
entrada11010 (binary)输出1A (hex) ✓
Resultado esperado
El mismo numeric valor represented en el target radix, con todo digits válido para ese base.
Errores comunes
- 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.
Solución de problemas指引
- 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.
Límites de seguridad
- Pure arithmetic conversion with no security implications.