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

2023-12-25 14:54:40 浏览数 (3)

话不多说 直接上代码 CV可用!

代码语言:javascript复制
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 是不是很神奇!!!

1 人点赞