小程序 ES常用的一些数据转换

// ArrayBuffer转16进度字符串示例(无符号)
function ab2hex(buffer) {
  return Array.prototype.map.call(buffer, x => (
    Math.floor((x >>> 32).toString(16).length % 2 != 0) ? (`${"0"}${(x >>> 32).toString(16)}`) : (x >>> 32).toString(16))).join('').toLocaleUpperCase();
}

// 16进制字符串转ArrayBuffer补齐20bit
function hex2abFill(value) {
  var typedArray = new Uint8Array(value.match(/[\da-f]{2}/gi).map(function (h) {
    return parseInt(h, 16)
  }))
  var buffer = typedArray.buffer;
  var bufLen = buffer.byteLength;
  if (Math.floor(bufLen % 20) != 0) {
    var fillBuffer = new Uint8Array(Math.ceil(bufLen / 20) * 20);
    fillBuffer.set(typedArray);
    buffer = fillBuffer.buffer;
  }
  return buffer;
}

// 16进制字符串转ArrayBuffer不处理
function hex2ab(value) {
  var typedArray = new Uint8Array(value.match(/[\da-f]{2}/gi).map(function (h) {
    return parseInt(h, 16);
  }))
  return typedArray.buffer;
}

/**
 * u32转HEX
 */
function uint32ArrayToHex(arrayUint32) {
  let u32Hex = "";
  let srcLen = arrayUint32.length;
  for (let h = 0; h < srcLen; h++) {
    let valHex = (arrayUint32[h] >>> 32).toString(16);
    let valLen = valHex.length;
    let hexFill = ""
    let fillLen = 8 - valLen;
    for (let i = 0; i < fillLen; i++) {
      hexFill = `${hexFill}${"0"}`
    }
    u32Hex = `${u32Hex}${hexFill}${valHex}`
  }
  return u32Hex.toLocaleUpperCase();
}

/**
 *byte[] 数组转十六进制字符串
 * @param strValue byte[] 数组
 * @returns {string}
 */
function byteArrayToHex(arrayValue) {
  var hexStr = "";
  for (var arrayItem of arrayValue) {
    var charHex = arrayItem.toString(16);
    charHex = (Math.floor(charHex.length % 2) == 0) ? charHex : `0${charHex}`;
    hexStr = `${hexStr}${charHex}`;
  }
  return hexStr.toLocaleUpperCase();
}

/**
 * 十六进制字符串转byte[]
 * @param hexStrValue 十六进制字符串
 *
 */
function hexStrToByte(hexStrValue) {
  var hexValueLen = hexStrValue.length;
  var arrayCode = [];
  if (Math.floor(hexValueLen % 2) != 0) {
    hexStrValue = `${hexStrValue}0`;
  }
  for (var i = 0; i < hexValueLen; i += 2) {
    var itemValue = hexStrValue.substring(i, i + 2);
    arrayCode.push(parseInt(itemValue, 16))
  }
  return arrayCode;
}

/**
 * 整型字节补齐
 */
function intHexFill(inteValue, size) {
  let valLen = inteValue.toString(16).length;
  let retVal = "";
  let fillSize = size * 2;
  let fillStart = fillSize - valLen;
  for (var i = 0; i < fillSize; i++) {
    if (i == fillStart) {
      retVal = `${retVal}${inteValue.toString(16)}`
      break;
    } else {
      retVal = `${retVal}${"0"}`
    }
  }
  return retVal;
}

/**
 * 字符串转十六进制
 */
function StringToHex(strVal) {
  var strLen = strVal.length;
  let hexStr = "";
  for (var i = 0; i < strLen; i++) {
     let hexVal = strVal.charCodeAt(i).toString(16);
    hexStr = `${hexStr}${1 == hexVal.length ? `0${hexVal}` : hexVal}`;
  }
  return hexStr.toLocaleUpperCase();
}

/**
 * 十六进制数据转ASCII
 */
function hexStrToStr(hexStrValue) {
  var hexValueLen = hexStrValue.length;
  var strCode = "";
  if (Math.floor(hexValueLen % 2) != 0) {
    hexStrValue = `${hexStrValue}0`;
  }
  for (var i = 0; i < hexValueLen; i += 2) {
    var itemValue = hexStrValue.substring(i, i + 2);
    strCode = `${strCode}${String.fromCharCode(parseInt(itemValue, 16))}`
  }
  return strCode;
}
//数据拼包
function bufPin(normalArray, buffer) { // buffer is an ArrayBuffer
  var itemD = Array.prototype.map.call(new Uint8Array(buffer), x => (Math.floor(x.toString(16).length % 2 != 0) ? (`${"0"}${x.toString(16)}`) : x.toString(16))).join(' ');
  console.log("单包数据:" + itemD);
  let npIndex = new Uint8Array(buffer);
  if (1 == npIndex[0]) {
    normalArray = []
  }
  var typedArray = new Uint8Array(buffer, 1)
  var moreVegs = Array.apply([], typedArray);
  Array.prototype.push.apply(normalArray, moreVegs);
  return normalArray;
}

/**
 * Unicode编码转换十六进制
 */
function UnicodeToHex(uncodeVal){
  let str = unescape(uncodeVal.replace(/\u/g, "%u"));
  return StringToHex(str);
}

//给发送的数据添加包头(每20个添加一个编号),总长度拼成20的倍数
function sendMsgAddBaotou(hexStrValue) {
  let arrayVal = hexStrToByte(hexStrValue);
  let arrLen = arrayVal.length;
  let retHex = "";
  let retArr = [];
  let btIndex = 1;
  for (var i = 0; i < arrLen; i++) {
    if (0 == Math.floor(i % 19)) {
      retArr.push(btIndex)
      btIndex++;
    }
    retArr.push(arrayVal[i]);
  }
  return byteArrayToHex(retArr);
}

/**
 * 异或0x5a
 */
function XOR_0X5A(b) {
  let temp = 0;
  let len = b.length;
  for (var i = 0; i < len; i++) {
    temp = b[i] ^ 90;
    b[i] = temp;
  }
  return b;
}
// 按位异或
function XOR(b, len) {
  var tmp = b[0];
  for (var i = 1; i < len; i++) {
    tmp ^= b[i];
  }
  return tmp;
}

// byte[]转整形
function bytesToLong(bytValue) {
  let ret = parseInt(ab2hex(bytValue), 16);
  return ret;
}

/**
 * byte[] 转字符串
 * @param byteArrayValue 数组
 * @returns {string}
 */
function byteArrayToStr(byteArrayValue) {
  let unit8Arr = new Uint8Array(byteArrayValue);
  let encodedString = String.fromCharCode.apply(null, unit8Arr);
  // 防止中文乱码
  let decodedString = decodeURIComponent(escape((encodedString)));
  return decodedString;
}

/**
 * 十六进制字符串转byte[]
 * @param hexStrValue 十六进制字符串
 *
 */
function hexStrToByte(hexStrValue) {
  var hexValueLen = hexStrValue.length;
  var arrayCode = [];
  if (Math.floor(hexValueLen % 2) != 0) {
    hexStrValue = `${hexStrValue}0`;
  }
  for (var i = 0; i < hexValueLen; i += 2) {
    var itemValue = hexStrValue.substring(i, i + 2);
    arrayCode.push(parseInt(itemValue, 16))
  }
  return arrayCode;
}


module.exports = {
  ab2hex: ab2hex,
  hex2ab: hex2ab,
  bufPin: bufPin,
  XOR: XOR,
  XOR_0X5A: XOR_0X5A,
  bytesToLong: bytesToLong,
  hex2abFill: hex2abFill,
  byteArrayToStr: byteArrayToStr,
  hexStrToByte: hexStrToByte,
  StringToHex: StringToHex,
  uint32ArrayToHex: uint32ArrayToHex,
  intHexFill: intHexFill,
  hexStrToStr: hexStrToStr,
  sendMsgAddBaotou: sendMsgAddBaotou,
  byteArrayToHex: byteArrayToHex,
  UnicodeToHex:UnicodeToHex
}

你可能感兴趣的:(微信小程序)