16进制字符串转字符串[hex]
// 16进制转字符串
hexToString: function(value) {
let arr = this.groupArray(value), //将16进制字符串进行每两个分组
result = "";
for (var i = 0; i < arr.length; i++) {
result += String.fromCharCode(parseInt(arr[i], 16)); //将分组后的16进制字符串转10进制Unicode码,然后将Unicode码转换为字符
}
return result
},
groupArray: function(str) {
let result = new Array();
for (let i = 0; i < str.length / 2; i++) {
var currentValue = str.slice(i * 2, (i + 1) * 2);`在这里插入代码片`
result.push(currentValue);
}
return result;
},
字符串转16进制字符串[string]
/**
* 转成ascii码数组
*/
function hexToArray(hexStr) {
var words = [];
var hexStrLength = hexStr.length;
if (hexStrLength % 2 !== 0) {
hexStr = leftPad(hexStr, hexStrLength + 1);
}
hexStrLength = hexStr.length;
for (var i = 0; i < hexStrLength; i += 2) {
words.push(parseInt(hexStr.substr(i, 2), 16));
}
return words;
}
/**
* 转成16进制串
*/
function arrayToHex(arr) {
var words = [];
var j = 0;
for (var i = 0; i < arr.length * 2; i += 2) {
words[i >>> 3] |= parseInt(arr[j], 10) << 24 - i % 8 * 4;
j++;
}
// 转换到16进制
var hexChars = [];
for (var _i2 = 0; _i2 < arr.length; _i2++) {
var bite = words[_i2 >>> 2] >>> 24 - _i2 % 4 * 8 & 0xff;
hexChars.push((bite >>> 4).toString(16));
hexChars.push((bite & 0x0f).toString(16));
}
return hexChars.join('');
}
来源:CSDN
作者:赵自煜(赵超)
链接:https://blog.csdn.net/zhaoshidaye666/article/details/103291664