如何将文本转换为\ x代码?

Vin*_*Vin 5 php hex encode

我想将普通文本转换为\ x代码,例如\ x14\x65\x60

例如 :

normal text = "base64_decode"
converted \x codes for above text = "\x62\141\x73\145\x36\64\x5f\144\x65\143\x6f\144\x65"
Run Code Online (Sandbox Code Playgroud)

这该怎么做?提前致谢.

Eve*_*ert 5

ord()函数为您提供单个字节的十进制值.dechex()将其转换为十六进制.所以要做到这一点,循环遍历字符串中的每个字符并应用这两个函数.


san*_*mai 5

PHP 5.3单行代码:

echo preg_replace_callback("/./", function($matched) {
    return '\x'.dechex(ord($matched[0]));
}, 'base64_decode');
Run Code Online (Sandbox Code Playgroud)

产出 \x62\x61\x73\x65\x36\x34\x5f\x64\x65\x63\x6f\x64\x65