In JavaScript, it is easy to convert binary, decimal, hexadecimal and octal. The following is a detailed introduction to the code examples and principles of each conversion.
1. Decimal to other divisions
Decimal to binary
use(2)
Method converts decimal numbers into binary strings.
const decimalNumber = 10; const binaryString = (2); (binaryString); // Output: '1010'
explain:toString()
The method is in JavaScriptNumber
A method of an object that accepts an optional parameterradix
, represents the converted binary, with a value range of 2 to 36. whenradix
When is 2, the decimal number is converted to a binary string.
Decimal to octal
use(8)
Method converts decimal numbers into octal strings.
const decimalNumber = 10; const octalString = (8); (octalString); // Output: '12'
explain: Similarly, whenradix
When 8,toString()
The method will convert a decimal number into an octal string.
Decimal to hexadecimal
use(16)
Method converts decimal numbers into hexadecimal strings.
const decimalNumber = 255; const hexadecimalString = (16); (hexadecimalString); // Output: 'ff'
explain:whenradix
When 16,toString()
The method will convert decimal numbers into hexadecimal strings, of which 10 - 15 will use lettersa
- f
express.
2. Other Category to Decimal
Binary to decimal
useparseInt()
Function, converting binary strings to decimal numbers.
const binaryString = '1010'; const decimalNumber = parseInt(binaryString, 2); (decimalNumber); // Output: 10
explain:parseInt()
The function accepts two parameters. The first parameter is the string to be converted, and the second parameter is the binary of the string. When the second parameter is 2, the binary string is converted to a decimal number.
Octal to decimal
useparseInt()
Function, convert an octal string to a decimal number.
const octalString = '12'; const decimalNumber = parseInt(octalString, 8); (decimalNumber); // Output: 10
explain:whenparseInt()
When the second parameter of the function is 8, the octal string will be converted to a decimal number.
Hexadecimal to decimal
useparseInt()
Function, convert a hexadecimal string to a decimal number.
const hexadecimalString = 'ff'; const decimalNumber = parseInt(hexadecimalString, 16); (decimalNumber); // Output: 255
explain:whenparseInt()
When the second parameter of the function is 16, the hexadecimal string will be converted to a decimal number.
3. Conversion between binary, octal and hexadecimal
You can first convert the source to decimal, and then convert the decimal to the target.
Binary to octal
const binaryString = '1010'; const decimalNumber = parseInt(binaryString, 2); const octalString = (8); (octalString); // Output: '12'
explain: Use firstparseInt()
The function converts a binary string to a decimal number and then usestoString(8)
Method converts decimal numbers into octal strings.
Binary to hexadecimal
const binaryString = '11111111'; const decimalNumber = parseInt(binaryString, 2); const hexadecimalString = (16); (hexadecimalString); // Output: 'ff'
explain: First convert the binary string to a decimal number, and then convert the decimal number to a hexadecimal string.
Octal to binary
const octalString = '12'; const decimalNumber = parseInt(octalString, 8); const binaryString = (2); (binaryString); // Output: '1010'
explain: First convert the octal string to a decimal number, and then convert the decimal number to a binary string.
Octal to hexadecimal
const octalString = '377'; const decimalNumber = parseInt(octalString, 8); const hexadecimalString = (16); (hexadecimalString); // Output: 'ff'
explain: First convert the octal string to a decimal number, and then convert the decimal number to a hexadecimal string.
Hexadecimal to binary
const hexadecimalString = 'ff'; const decimalNumber = parseInt(hexadecimalString, 16); const binaryString = (2); (binaryString); // Output: '1111111'
explain: First convert the hexadecimal string to a decimal number, and then convert the decimal number to a binary string.
Hexadecimal to octal
const hexadecimalString = 'ff'; const decimalNumber = parseInt(hexadecimalString, 16); const octalString = (8); (octalString); // Output: '377'
explain: First convert the hexadecimal string to a decimal number, and then convert the decimal number to an octal string.
To sum up, bytoString()
Methods andparseInt()
Functions can easily convert binary, decimal, hexadecimal and octal in JavaScript.
Summarize
This is the article about JavaScript's implementation of mutual conversion between binary, decimal, hexadecimal and octal. For more related contents of JS binary, decimal, hexadecimal and octal conversion, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!