Javascript 10进制转16进制补0 ,16进制转10进制
10进制转16进制补0
function dec2hex(dec, len) {
var hex = "";
while( dec ) {
var last = dec & 15;
hex = String.fromCharCode(((last>9)?55:48)+last) + hex;
dec >>= 4;
}
if(len) {
while(hex.length < len) hex = '0' + hex;
}
return hex;
}
16进制转10进制
function string_hex2int(hex) {
var len = hex.length, a = new Array(len), code;
for (var i = 0; i < len; i++) {
code = hex.charCodeAt(i);
if (48<=code && code < 58) {
code -= 48;
} else {
code = (code & 0xdf) - 65 + 10;
}
a[i] = code;
}
return a.reduce(function(acc, c) {
acc = 16 * acc + c;
return acc;
}, 0);
}