在此记录遇到的问题,若遇到同样的问题,希望可以帮到各位。
我从蓝牙设备获取的数据为16进制 需要转为中文 同样还有另一种16进制 unicode16进制数据 搜索的大部分都是直接转换的方法 我都一一尝试 可能是搜索的姿势不对吧 只好自己解决咯 咬牙切齿骂c开发
首先将我获取的16进制全部转为 2进制 啊哈哈哈 我不知道有没有快捷的函数转法 反正我是自己写的
//16进制转2进制
changeTobinary: function (temperature) {
var charary = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
var numary = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
var binarynum = '';
// 检索对应16进制的值
for (var i = 0; i < temperature.length; i++) {
var num = 0;
var cons = temperature.substring(i, i + 1)
for (var t = 0; t < charary.length; t++) {
if (cons == charary[t]) {
num = numary[t];
}
}
var binary = '';
while (num > 0) {
var yu = num % 2;
num = parseInt(num / 2);
binary = yu + binary;
}
while (binary.length < 4) {
binary = '0' + binary;
}
binarynum = binarynum + binary;
}
while (binarynum.length < 8) {
binarynum = '0' + binarynum;
}
var splthree = '';
splthree = binarynum;
return splthree;
},
别问我啥意思 写完之后 我就忘了啥意思 哈哈哈
这里是我最纠结的东西了
2进制转unicode2进制
以及unicode2进制转unicode16进制 最后转为中文 直接上代码
utf8changeunicode: function (binarylist) {
var endindex = 0;//判断需要循环几次后存入数据
var binarylistcope = [];//存储处理后的数据
var endbinarycope = '';//存储处理后的数据
// 循环取出数组中的二进制
var i = 0;
while (i < binarylist.length) {
// 循环判断字符串否和标准
var t = 0;//计数t
var str = binarylist[i];
while (t < str.length) {
var index = str.substring(t, t + 1);
// 如果符合规则 = 1 计数+1 否则跳出判断
if (index == 0) {
break;
}
else {
t++;
}
}
var end = t + 1; //判断需要截取的index
if (endindex == 0) {
endindex = t;
endbinarycope = '';
}
var endstr = str.substring(end, str.length);
if (endindex == 1) {
endbinarycope += endstr;
binarylistcope.push(endbinarycope);
endindex--;
}
else if (endindex == 0) {
endbinarycope += endstr;
binarylistcope.push(endbinarycope);
}
else {
endbinarycope += endstr;
endindex--;
}
i++
}
var unicodelist = this.changehexadecimal(binarylistcope);
console.log(unicodelist);
var stringlist = [];
for (var a = 0; a < unicodelist.length; a++) {
while (unicodelist[a].length < 4) {
unicodelist[a] = '0' + unicodelist[a];
}
stringlist.push('\\u' + unicodelist[a]);
}
console.log(stringlist);
var namedev = '{"str":"';
for (var i = 0; i < stringlist.length; i++) {
namedev += stringlist[i];
}
namedev = namedev + '"}';
return JSON.parse(namedev).str
},
changehexadecimal: function (binarylist) {
var charary = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
var numary = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
var endlist = [];
for (var i = 0; i < binarylist.length; i++) {
var str = binarylist[i];
var numb = str.length;
while (numb % 4 != 0) {
str = '0' + str;
numb = str.length;
}
var endstr = '';
for (var t = 0; t < str.length / 4; t++) {
var cons = str.substring(t * 4, t * 4 + 4);
var cc = 0;
for (var k = 0; k < cons.length; k++) {
var cont = parseInt(cons.substring(k, k + 1));
var num = cons.length - k - 1;
var kf = 2;
if (num == 0) {
kf = 1;
}
else if (num == 1) {
kf = 2;
}
else {
while (num - 1 >= 1) {
kf = 2 * kf;
--num;
}
}
cc = kf * cont + cc;
}
for (var k = 0; k < numary.length; k++) {
if (cc == numary[k]) {
cc = charary[k];
}
}
endstr += cc;
}
endlist.push(endstr)
}
return endlist;
},
到此记录完成