Created
May 17, 2019 01:56
-
-
Save kiinlam/176ce20707336fa8278726e869e59cb1 to your computer and use it in GitHub Desktop.
Javascript \x 反斜杠x 16进制 编解码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// js 里 \x 开头的通常是16进制编码的数据,下面代码实现编解码: | |
// 解码 | |
//eg. | |
//decode('\x5f\x63\x68\x61\x6e\x67\x65\x49\x74\x65\x6d\x43\x72\x6f\x73\x73\x4c\x61\x79\x65\x72') | |
//"_changeItemCrossLayer" | |
function decode(str){ | |
return str.replace(/\\x(\w{2})/g,function(_,$1){ return String.fromCharCode(parseInt($1,16)) }); | |
} | |
//编码 | |
//eg. | |
//encode("_changeItemCrossLayer") | |
//"\x5f\x63\x68\x61\x6e\x67\x65\x49\x74\x65\x6d\x43\x72\x6f\x73\x73\x4c\x61\x79\x65\x72" | |
function encode(str){ | |
return str.replace(/(\w)/g,function(_,$1){ return "\\x"+ $1.charCodeAt(0).toString(16) }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment