JS 十六进制字符串转十六进制字符串数组以及字节数组

JS 十六进制字符串转十六进制字符串数组以及字节数组

//十六进制字符串转换为数组
function hexStringToArray(str) {
  var pos = 0;
  var len = str.length;
  if (len % 2 != 0) {
    return null;
  }
  len /= 2;
  var arrBytes = new Array();
  for (var i = 0; i < len; i++) {
    var s = str.substr(pos, 2);
    arrBytes.push(s);
    pos += 2;
  }
  return arrBytes;
}
//十六进制字符串转换为字节数组
function HexString2Bytes(str) {
  var pos = 0;
  var len = str.length;
  if (len % 2 != 0) {
    return null;
  }
  len /= 2;
  var arrBytes = new Array();
  for (var i = 0; i < len; i++) {
    var s = str.substr(pos, 2);
    var v = intToByte(parseInt(s, 16));
    arrBytes.push(v);
    pos += 2;
  }
  return arrBytes;
}
/**
 * 把 ArrayBuffer 转换成 16 进制内容
 * @param buffer 二进制内容
 */
const ab2hex = buffer => {
  const arr = [] as any;
  Array.prototype.map.call(new Uint8Array(buffer), function(bit) {
    let item = ("00" + bit.toString(16)).slice(-2);
    arr.push(item);
  });
  return arr;
};

你可能感兴趣的:(前端小知识,javascript)