返回列表

hexEncode 和 hexDecode

默认分类 2019/01/23 03:29

原文地址:https://stackoverflow.com/questions/21647928/javascript-unicode-string-to-hex

hexEncode 和 hexDecode:

String.prototype.hexEncode = function(){
    var hex, i;

    var result = "";
    for (i=0; i<this.length; i++) {
        hex = this.charCodeAt(i).toString(16);
        result += ("000"+hex).slice(-4);
    }

    return result
}
Back again:

String.prototype.hexDecode = function(){
    var j;
    var hexes = this.match(/.{1,4}/g) || [];
    var back = "";
    for(j = 0; j<hexes.length; j++) {
        back += String.fromCharCode(parseInt(hexes[j], 16));
    }

    return back;
}


>> 留言评论