Files

24 lines
1018 B
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.
// 解析通信服务的通信数据
module.exports = (type, msg) => {
let regex, arr, result = '';
if (type == '0000') {
// 0000, 汉字编码,按14bit区位编码,前7位为区码,后7位为位码
// 区号和位号分别加上160,再分别转换成十六进制数,就成为四位的十六进制GB2312国家标准编码
regex = /.......?/g;
arr = msg.match(regex);
for (let t = 0; t < arr.length; t += 2) {
result = result + (parseInt(arr[t], 2) + 160).toString(16) + (parseInt(arr[t + 1], 2) + 160).toString(16);
}
} else if (type == '0001') {
// 0001,BCD码,按4bit编码,将每4bit二进制转换为一个十进制数,再拼接成字符串
regex = /....?/g;
arr = msg.match(regex);
arr.forEach(element => {
result = result + parseInt(element, 2);
});
} else {
// 保留,二进制转为十进制
result = parseInt(msg, 2);
}
return result;
};