Number Base Converter
Binary, octal, decimal and hex, at any width.
Binary, octal, decimal and hex, at any width.
How to use it
- Paste the value. A 0x or 0b prefix is stripped for you, and underscores are ignored.
- Set the two bases. Binary, octal, decimal or hexadecimal, in either direction.
- Copy the result. Hexadecimal comes back in upper case, which is how it is usually written.
When you would use this
Base conversion is simple until the number gets large, and then most tools quietly break. The usual implementation is parseInt, which has two failures that matter here. It loses precision above 2 to the 53, which is roughly 9 quadrillion, and a base converter is precisely where a 64 bit register value or a hash gets pasted. And it stops at the first character it does not recognise, returning what it read so far, so a typo in the middle of a value produces a confidently wrong answer with no warning. This uses arbitrary precision integers, so a 64 bit value converts exactly, and it validates every digit against the base before converting anything, naming the character that does not belong. Prefixes and grouping characters are handled rather than rejected. A value copied out of source code with an 0x on the front or underscores in the middle converts as pasted.
Questions
- Does it handle 64 bit values?
- Yes, at full precision. Most converters use parseInt, which silently loses accuracy above about 9 quadrillion, and a base converter is exactly where someone pastes a 64 bit register value. This uses arbitrary precision integers, so the answer is exact at any width.
- What happens to a digit that does not belong to the base?
- It is named and the conversion is refused. parseInt stops at the first character it does not recognise and returns whatever it read up to that point, so 12x3 comes back as 12 with no indication anything was wrong.
- Can I paste a value with a prefix?
- Yes. A leading 0x, 0b or 0o is stripped, and underscores or spaces used as grouping are ignored, so a value copied out of source code works without being cleaned up first.