JavaScript的16进制转10进制
程序员文章站
2024-03-18 20:46:16
...
JavaScript的16进制转10进制
JavaScript的16进制转10进制
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>js的16进制转10进制</title>
</head>
<body>
<script>
// 形参是 16进制 比如 "B7 D4"
function demo(hex) {
// 传入"B7 D4", 翻转去除空格"4D7B", 结果19835
console.log("传入的值: ", hex);
let str = hex.split('').reverse().join('');
console.log("翻转的值: ", str);
let decimalism = str.replace(/\s/g,"");
console.log("去除空格: ", decimalism);
let res = parseInt(decimalism, 16)
console.log("16转10的值: ", parseInt(decimalism, 16));//十六进制转十进制
return res
}
demo("B7 D4")
</script>
</body>
</html>
结果