JS is a very magical language, the internal system of many functions can help us make a number (into) system conversion;
JS can be directly used in the 16 system;
var a = 0xFF; 255
Converts arbitrary feed strings to decimal, such as binary, octal, hexadecimal, and number two, which is the most commonly used conversion to integer decimal;
The code is as follows:
parseint ("11", 2); 3 2-in-turn 10-in-system
parseint ("77", 8); 63 8-in-turn 10-in-system
parseint ("AF", 16); 175 16-in-turn 10-in-system
Converts 10 to 2, octal, hexadecimal strings
Object.ToString (N): Can (n) represent the system, such as
The code is as follows:
(152). ToString (2)//"10011000"; First, use parentheses to convert 152 to "package" into an object, or the following;
152..toString (2)//Here is the first point to convert 152 to float type decimal, and the second is to draw the object method;
152..toString (16)//"98": Decimal Turn 16 system
152..toString//"4o": Ten obtained turn 32
The same JavaScript support maximum feed is 36 (26 English letters + 10 digits)
35..toString//"Z": Maximum encoding "Z" is supported, case-insensitive
If necessary in the process of conversion. You can use the following methods:
The code is as follows:
/**
* @param num needs to be padded with 16 digits
* The number of digits @param len to be padded here is
* @returns a padded 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;
}