Javascript x 反斜杠x 16进制 编解码

js 里 x 开头的通常是16进制编码的数据,下面代码实现编解码:

解码

function decode(str){
	return str.replace(/\x(w{2})/g,function(_,$1){ return String.fromCharCode(parseInt($1,16)) });
}

eg.

decode('x5fx63x68x61x6ex67x65x49x74x65x6dx43x72x6fx73x73x4cx61x79x65x72')

"_changeItemCrossLayer"

编码

function encode(str){
    return str.replace(/(w)/g,function(_,$1){ return "\x"+ $1.charCodeAt(0).toString(16) });
}

eg.

encode("_changeItemCrossLayer")

"x5fx63x68x61x6ex67x65x49x74x65x6dx43x72x6fx73x73x4cx61x79x65x72"

延伸:

python将x 开头编码的数据解码成中文

原文地址:https://www.cnblogs.com/xiaoqi/p/js-x-encode-decode.html