Convertir Numbers Between Radices
Convertir integers between binary, octal, decimal, hexadecimal, and arbitrary radices.
问题
You avoir un nombre en one base (e.g., hex) et besoin it en another (e.g., binary ou decimal) pour un différent outil ou context.
Prérequis
- Understanding of positional numeral systems
- Basic arithmetic
Étapes
-
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.
entréesource: 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.
entrée1A (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).
entrée11010 (binary)输出1A (hex) ✓
Résultat attendu
Le même numeric valeur represented en le target radix, avec tout digits valide pour ce base.
Erreurs courantes
- 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.
Dépannage指引
- 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.
Limites de sécurité
- Pure arithmetic conversion with no security implications.