JavaScript/进制:修订间差异
来自康健生活
小 文字替换 -“{{Leftrightflex|”替换为“{{Double-columns|” |
|||
| (未显示同一用户的2个中间版本) | |||
| 第1行: | 第1行: | ||
== 引言 == | == 引言 == | ||
== 十进制转十六进制 == | == 十进制转十六进制 == | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
var a = 19968; | var a = 19968; | ||
a.toString(16);</syntaxhighlight>|{{Consoleout|<pre> | a.toString(16);</syntaxhighlight>|{{Consoleout|<pre> | ||
| 第7行: | 第7行: | ||
</pre>}}}} | </pre>}}}} | ||
也可以简化写法为 | 也可以简化写法为 | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
19968..toString(16);</syntaxhighlight>|{{Consoleout|<pre> | 19968..toString(16);</syntaxhighlight>|{{Consoleout|<pre> | ||
"4e00" | "4e00" | ||
| 第49行: | 第49行: | ||
console.log(hexData.toString("utf-8")); | console.log(hexData.toString("utf-8")); | ||
// => N</syntaxhighlight> | // => N</syntaxhighlight> | ||
== 十六进制字符串转整数 == | |||
{{Double-columns| | |||
<syntaxhighlight lang="javascript"> | |||
function 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); | |||
}hex2int('4e00') | |||
</syntaxhighlight>|{{consoleout|<pre> | |||
19968</pre>}}}} | |||
== 十进制整数转换十六进制 == | |||
<syntaxhighlight lang="javascript"> | |||
function int2hex(num, width) { | |||
var hex = "0123456789abcdef"; | |||
var s = ""; | |||
while (num) { | |||
s = hex.charAt(num % 16) + s; | |||
num = Math.floor(num / 16); | |||
} | |||
if (typeof width === "undefined" || width <= s.length) { | |||
return "0x" + s; | |||
} | |||
var delta = width - s.length; | |||
var padding = ""; | |||
while(delta-- > 0) { | |||
padding += "0"; | |||
} | |||
return "0x" + padding + s; | |||
} | |||
int2hex(479, 8);</syntaxhighlight> | |||
2026年11月26日 (四) 08:46的最新版本
引言
十进制转十六进制
var a = 19968;
a.toString(16);
"4e00"
也可以简化写法为
19968..toString(16);
"4e00"
function toHex(num){
var rs = "";
var temp;
while(num/16 > 0){
temp = num%16;
rs = (temp+"").replace("10","a").replace("11","b").replace("12","c").replace("13","d").replace("14","e").replace("15","f") + rs;
num = parseInt(num/16);
}
return rs;
}
十六进制转十进制
65535 除 16,余数 15(F),商4095 4095 除 16,余数 15(F),商255 255 除 16,余数 15(F),商15 15 除16,余数 15(F),商0,结束 得16进制为 FFFF 15*Math.pow(16,3)+15*Math.pow(16,2)+15*Math.pow(16,1)+15*Math.pow(16,0);
// 错误示例,没有改完
var num = 0;
'4e00'.replace(/(.)(?=[^$])/g, "$1,").split(",").forEach(function(d, i){
//d.charCodeAt(1).toString(); // 转为二进制
const parseIntD = parseInt(d, 16);
num += parseIntD*Math.pow(16,('4e00'.length - i - 1));
})
十六进制转二进制
注:十六进制字节如果超过13位会出现丢失
十六进制转字符串
var data = '4e00'
var hexData = new Buffer(data,'hex');
console.log(hexData.toString("utf-8"));
// => N
十六进制字符串转整数
function 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);
}hex2int('4e00')
19968
十进制整数转换十六进制
function int2hex(num, width) {
var hex = "0123456789abcdef";
var s = "";
while (num) {
s = hex.charAt(num % 16) + s;
num = Math.floor(num / 16);
}
if (typeof width === "undefined" || width <= s.length) {
return "0x" + s;
}
var delta = width - s.length;
var padding = "";
while(delta-- > 0) {
padding += "0";
}
return "0x" + padding + s;
}
int2hex(479, 8);
No categories