utools_plugin_impress_tools/js/hex_to_int_16bit.js
impressionyang 3a38835a15 audio(pcm): add pcm convert
添加了简单的16bit PCM数据采样i但转换功能
  1. 添加页面跳转:html\uac_pcm_data_convert.html
  2. 添加hex_to_int的带符号转换方法
end of segment
🔧 web(switch): add web pages switch feature
 添加了页面跳转的框架
  1. 添加页面跳转功能
  2. 添加主页相关的内容以及欢迎页面
end of segment
 utools(plugin): add utools plugin support
 添加了utools的插件功能
  1. 添加插件配置
  2. 添加插件的图标
end of segment
2022-06-17 00:19:28 +08:00

34 lines
1.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function hex2int(hexStr) {
let twoStr = parseInt(hexStr, 16).toString(2); // 将十六转十进制再转2进制
let bitNum = hexStr.length * 4; // 1个字节 = 8bit 0xff 一个 "f"就是4位
if (twoStr.length < bitNum) {
while (twoStr.length < bitNum) {
twoStr = "0" + twoStr;
}
}
if (twoStr.substring(0, 1) == "0") {
// 正数
twoStr = parseInt(twoStr, 2); // 二进制转十进制
return twoStr;
} else {
// 负数
let twoStr_unsign = "";
// console.log("hex2int--->" + parseInt(twoStr, 2));
twoStr = parseInt(twoStr, 2) - 1; // 补码:(负数)反码+1符号位不变相对十进制来说也是 +1但这里是负数+1就是绝对值数据-1
// console.log("hex2int--->" + twoStr);
twoStr = twoStr.toString(2);
twoStr_unsign = twoStr.substring(1, bitNum); // 舍弃首位(符号位)
// 去除首字符将0转为1将1转为0 反码
twoStr_unsign = twoStr_unsign.replace(/0/g, "z");
twoStr_unsign = twoStr_unsign.replace(/1/g, "0");
twoStr_unsign = twoStr_unsign.replace(/z/g, "1");
// console.log("hex2int--->" + twoStr_unsign);
// console.log("hex2int--->" + (-twoStr_unsign));
twoStr = parseInt(-twoStr_unsign, 2);
return twoStr;
}
}