js 进制转换:十六进制转十进制、十进制转十六进制、十六进制转ASCII码、
因为近期做小程序,蓝牙连接硬件,需要根据module bus通信协议解析数据,用到了很多标题的算法转换,借此总结一下。
十六进制 转 十进制
function hex2dec(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);
}
十进制 转 十六进制
function dec2hex(val) {
return ("0" + (Number(val).toString(16))).toUpperCase()
//return ("0" + (Number(val).toString(16))).slice(-2).toUpperCase()
}
十六进制 转 二进制 (需要借用到hex2dec,也就是需要先转换成十进制,再转换成二进制)
function hex2bin(hex, len) {
var res = hex_dec(hex).toString(2);
return (Array(len).join(0) + res).slice(-len);
}
二进制 转 十六进制(网上的一种办法)
function bin2hex(str) {
let hex_array = [{ key: 0, val: "0000" }, { key: 1, val: "0001" }, { key: 2, val: "0010" }, { key: 3, val: "0011" }, { key: 4, val: "0100" }, { key: 5, val: "0101" }, { key: 6, val: "0110" }, { key: 7, val: "0111" },
{ key: 8, val: "1000" }, { key: 9, val: "1001" }, { key: 'a', val: "1010" }, { key: 'b', val: "1011" }, { key: 'c', val: "1100" }, { key: 'd', val: "1101" }, { key: 'e', val: "1110" }, { key: 'f', val: "1111" }]
let value = ''
let list = []
if (str.length % 4 !== 0) {
let a = "0000"
let b = a.substring(0, 4 - str.length % 4)
str = b.concat(str)
}
while (str.length > 4) {
list.push(str.substring(0, 4))
str = str.substring(4);
}
list.push(str)
for (let i = 0; i < list.length; i++) {
for (let j = 0; j < hex_array.length; j++) {
if (list[i] == hex_array[j].val) {
value = value.concat(hex_array[j].key)
break
}
}
}
return value
}
十六进制 转 ArrayBuffer
function hex2buf(hexStr) {
var count = hexStr.length / 2;
let buffer = new ArrayBuffer(count);
let dataView = new DataView(buffer);
for (var i = 0; i < count; i++) {
var curCharCode = parseInt(hexStr.substr(i * 2, 2), 16);
dataView.setUint8(i, curCharCode);
}
return buffer;
}
ArrayBuffer 转 十六进制
function buf2hex(buffer) { // buffer is an ArrayBuffer
return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
}
十六进制 转 ASCII码
function hex2ascll(hexCharCodeStr) {
var trimedStr = hexCharCodeStr.trim();
var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr;
var len = rawStr.length;
if (len % 2 !== 0) {
alert("存在非法字符!");
return "";
}
var curCharCode;
var resultStr = [];
for (var i = 0; i < len; i = i + 2) {
curCharCode = parseInt(rawStr.substr(i, 2), 16);
resultStr.push(String.fromCharCode(curCharCode));
}
return resultStr.join("");
}
字符串 转 byte
function string2byte(str) {
var ch, st, re = [];
for (var i = 0; i < str.length; i++) {
ch = str.charCodeAt(i); // get char
st = []; // set up "stack"
do {
st.push(ch & 0xFF); // push byte to stack
ch = ch >> 8; // shift value down by 1 byte
}
while (ch);
// add stack contents to result
// done because chars have "wrong" endianness
re = re.concat(st.reverse());
}
// return an array of bytes
return re;
}
本文来自博客园,作者:时光凉忆,转载请注明原文链接:https://www.cnblogs.com/naturl/p/15450061.html