JS is a very magical language. Many built-in functions can help us perform digital (regular) conversion;
Hexadecimal can be used directly in JS;
var a = 0xff; //255
Convert any binary string to decimal, such as binary, octal, hexadecimal, and the second number is not written, which is the most commonly used conversion to integer decimal;
The code copy is as follows:
parseInt("11", 2); // 3 2-digit to decimal
parseInt("77", 8); // 63 8-to-decimal
parseInt("af", 16); //175 Hexadecimal to decimal
Convert decimal to bin, octal, hexadecimal string
Object.toString(n): That is, (n) represents the binary system, such as
The code copy is as follows:
(152).toString(2) // "10011000" ; First use brackets to convert 152 to "package" into an object, or write it as follows;
152..toString(2) // Here the first point converts 152 into a decimal of type float, and the second point is to elicit the object method;
152..toString(16) // "98" : Decimal to hexadecimal
152..toString(32) // "4o": ten-lift system to 32-digit
Similarly, Javascript supports the maximum calculator as 36 (26 English letters + 10 numbers)
35..toString(36) // "z" : Supports maximum encoding "Z", case-insensitive
If it needs to be filled during the conversion process. You can use the following methods:
The code copy is as follows:
/**
* @param num 16 to be filled is the number
* @param len The number of digits to be filled here is
* @returns The completed string
* */
function format(num, len) {
var l = num.length;
if (num.length < len) {
for (var i = 0; i < len - l; i++) {
num = "0" + num;
}
}
return num;
}