ES6 JS 关于字符串和数组之间的转换

ES6 JS 关于字符串和数组之间的转换

// ArrayBuffer转16进度字符串示例
function ab2hex(buffer) {
  var hexArr = Array.prototype.map.call(
    new Uint8Array(buffer),
    function(bit) {
      return ('00' + bit.toString(16)).slice(-2)
    }
  )
  return hexArr.join('');
}

// 16进制字符串转ArrayBuffer
function hex2ab(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进制(只支持16进制的字符串)
 */
function strToHexCharCode(str) {  
  if (str === "") return "";  
  var hexCharCode = [];
  for (var i = 0; i < str.length; i++) {
    hexCharCode.push((str.charCodeAt(i)).toString(16));  
  }
  return hexCharCode.join("");
}

// 异或
function XOR(b, len) {
  var tmp = b[0];
  for (var i = 1; i < len; i++) {
    tmp ^= b[i];
  }
  return tmp;
}

// byte[]转整形
function bytesToLong(bytValue) {
  var ret = 0;
  for (var i = 0; i < bytValue.length; i++) {
    ret += (bytValue[bytValue.length - i - 1] & 0xFF) << (i * 8);
  }
  return ret;
}

/**
 * 十六进制转字符串
 */
function hexCharCodeToStr(hexCharCodeStr) {  
  var trimedStr = hexCharCodeStr.trim();  
  var rawStr = trimedStr;  
  var len = rawStr.length;  
  if (len % 2 !== 0) {
    console.log("Illegal Format ASCII Code!");
    return "";  
  }  
  var curCharCode;  
  var resultStr = [];  
  for (var i = 0; i < len; i = i + 2) {    
    curCharCode = parseInt(rawStr.substr(i, 2), 16); // ASCII Code Value
        
    resultStr.push(String.fromCharCode(curCharCode));  
  }  
  return resultStr.join("");
}

#//================================js=============================



 var str = "ABCD,;'/,][;'85*&%$#@!}{\nABCD,;'/,][;'';/.,<{]~`!85*&%$#@!}{\n";

    let intArray = strToIntArray(str);
    let toStr = byteArrayToStr(intArray);
    console.log("toStr==数组转字符串====" + toStr)
    let toHex = byteArrayToHex(intArray);
    console.log("===数组转十六进制字符串======" + toHex)
    let toByte = hexStrToByte(toHex);
    let toStrOld = byteArrayToStr(toByte);
    console.log("toStr==数组转字符串==toStrOld==" + toStrOld)

    /**
     *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;i2){
            var itemValue = hexStrValue.substring(i,i+2);
            arrayCode.push(parseInt(itemValue,16))
        }
        return arrayCode;
    }


    /**
     * byte[] 转字符串
     * @param byteArrayValue 数组
     * @returns {string}
     */
    function byteArrayToStr(byteArrayValue) {
        var str = "";
        for (var arrayItem of byteArrayValue) {
            str = `${str}${String.fromCharCode(arrayItem)}`;
        }
        return str;
    }

    /**
     * 字符串转byte[]
     * @param strValue ASCII字符串
     * @returns {Array}数组
     */
    function strToIntArray(strValue) {
        var intAray = [];
        var valueLen = strValue.length;
        for (var i = 0; i < valueLen; i++) {
            intAray.push(strValue.charCodeAt(i));
        }
        return intAray;
    }

//结果
toStr==数组转字符串====ABCD,;'/,][;'85*&%$#@!}{
ABCD,;'/,][;'';/.,<{]~`!85*&%$#@!}{

Conversion.html?_ijt=ol6i0r2lhnq35vp2tllh27lcug:19 ===数组转十六进制字符串======414243442C3B272F2C5D5B3B2738352A2625242340217D7B0A414243442C3B272F2C5D5B3B27273B2F2E2C3C7B5D7E602138352A2625242340217D7B0A
Conversion.html?_ijt=ol6i0r2lhnq35vp2tllh27lcug:22 toStr==数组转字符串==toStrOld==ABCD,;'/,][;'85*&%$#@!}{
ABCD,;'/,][;'';/.,<{]~`!85*&%$#@!}{

你可能感兴趣的:(ECMAScript)