16进制还能转字符串?横杠、标点全都有!!进来看看!!

话不多说 直接上代码 CV可用!!不好用来找我!!

export  function hexCharCodeToStr(hexCharCodeStr) {
    var trimedStr = hexCharCodeStr.trim();
    var rawStr =
      trimedStr.substr(0,2).toLowerCase() === "0x"
        ?
        trimedStr.substr(2)
        :
        trimedStr;
    var len = rawStr.length;
    if(len % 2 !== 0) {
        alert("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("");
}

hexCharCodeStr 即为你需要传入的16进制数据
例:let hexCharCodeStr = 54483930325f56312e312e315f3230323030333133
转换为字符串的结果为:TH902_V1.1.1_20200313
是不是很神奇!!!

你可能感兴趣的:(javascript,ES6,前端)