赞
踩
// 字符串转为ArrayBuffer对象,参数为字符串
const str2ab = function(str) {
var buf = new ArrayBuffer(str.length * 2); // 每个字符占用2个字节
var bufView = new Uint16Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
// ArrayBuffer转为字符串,参数为ArrayBuffer对象, 字符串编码为一个字节
const ab2str = function (buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
// ArrayBuffer转为字符串,参数为ArrayBuffer对象字符串编码为js默认编码
const ab2strU16 = function (buf) {
return String.fromCharCode.apply(null, new Uint16Array(buf));
}
/** * 十六进制转 bytearray */ var hex2ab = function(hex){ var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) { return parseInt(h, 16) })) var buffer = typedArray.buffer return buffer } // ArrayBuffer转16进度字符串示例 const ab2hex = function (buffer) { var hexArr = Array.prototype.map.call( new Uint8Array(buffer), function (bit) { return ('00' + bit.toString(16)).slice(-2) } ) return hexArr.join(''); }
function concatenate(resultConstructor, ...arrays) { let totalLength = 0; for (let arr of arrays) { totalLength += arr.length; } let result = new resultConstructor(totalLength); let offset = 0; for (let arr of arrays) { result.set(arr, offset); offset += arr.length; } return result; } 使用: totalmsg = concatenate(Uint8Array, totalmsg, submsg)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。