Do you want to convert a number from one base to another? Just keep reading this article.
Contents
Getting started
In JavaScript, a number has a super useful method toString(radix)
that converts its value in decimal to a string of that number in the specified radix.
On the contrary, the parseInt(string, radix)
function converts a string in the specified radix to a decimal number.
Go ahead!
Decimal to binary
const number = 123;
number.toString(2); // '1111011'
Decimal to hex
const number = 123;
number.toString(16); // '7b'
Decimal to octal
const number = 123;
number.toString(8); // '173'
Binary to decimal
parseInt('1111011', 2); // 123
Hex to decimal
parseInt('7b', 16); // 123
Octal to decimal
parseInt('173', 8); // 123
Universal conversion function
To convert a number in decimal to other bases and vice versa are very straightforward. To convert a number between other bases, such as binary to hex, requires a little more effort. But do not worry, it is not too hard to implement.
There are 2 steps:
- Convert the input number (or string) from the source base to decimal
- Convert that decimal number to the destination base
const convertNumberBetweenBases = ({ number, fromRadix, toRadix }) => {
const decimal = parseInt(number, fromRadix);
const toRadixNumber = decimal.toString(toRadix);
return toRadixNumber;
};
Let’s try with some examples!
Binary to hex
convertNumberBetweenBases({
number: '1111011',
fromRadix: 2,
toRadix: 16,
});
// '7b'
Binary to octal
convertNumberBetweenBases({
number: '1111011',
fromRadix: 2,
toRadix: 8,
});
// '173'
Hex to binary
convertNumberBetweenBases({
number: '7b',
fromRadix: 16,
toRadix: 2,
});
// '1111011'
Hex to octal
convertNumberBetweenBases({
number: '7b',
fromRadix: 16,
toRadix: 8,
});
// '173'
Octal to binary
convertNumberBetweenBases({
number: '173',
fromRadix: 8,
toRadix: 2,
});
// '1111011'
Octal to hex
convertNumberBetweenBases({
number: '173',
fromRadix: 8,
toRadix: 16,
});
// '7b'
Conclusion
The universal conversion function is useful, right?
In this article, you have seen how to use the toString()
method to convert a number to a string. If you want to see more other ways, you can read the article How to convert a number to a string in JavaScript.