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

时间:2021-03-03 00:25:47

I want to convert normal text to \x codes for e.g \x14\x65\x60

我想把普通文本转换成e的\x代码。g \ x14 \ x65 \ x60

For example :

例如:

normal text = "base64_decode"
converted \x codes for above text = "\x62\141\x73\145\x36\64\x5f\144\x65\143\x6f\144\x65"

How to do this? Thanks in advance.

如何做到这一点呢?提前谢谢。

4 个解决方案

#1


4  

PHP 5.3 one-liner:

PHP 5.3一行程序:

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

Outputs \x62\x61\x73\x65\x36\x34\x5f\x64\x65\x63\x6f\x64\x65

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

#2


5  

The ord() function gives you the decimal value for a single byte. dechex() converts it to hex. So to do this, loop through the every character in the string and apply both functions.

函数的作用是:给出单个字节的十进制值。dechex()将其转换为十六进制。为此,循环遍历字符串中的每个字符并应用这两个函数。

#3


3  

$str = 'base64_decode';
$length = strlen($str);
$result = '';

for ($i = 0; $i < $length; $i++) $result .= '\\x'.str_pad(dechex(ord($str[$i])),2,'0',STR_PAD_LEFT);

print($result);

#4


1  

Here's working code:

工作代码:

function make_hexcodes($text) {
    $retval = '';
    for($i = 0; $i < strlen($text); ++$i) {
        $retval .= '\x'.dechex(ord($text[$i]));
    }

    return $retval;
}

echo make_hexcodes('base64_decode');

See it in action.

看它的实际应用。

#1


4  

PHP 5.3 one-liner:

PHP 5.3一行程序:

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

Outputs \x62\x61\x73\x65\x36\x34\x5f\x64\x65\x63\x6f\x64\x65

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

#2


5  

The ord() function gives you the decimal value for a single byte. dechex() converts it to hex. So to do this, loop through the every character in the string and apply both functions.

函数的作用是:给出单个字节的十进制值。dechex()将其转换为十六进制。为此,循环遍历字符串中的每个字符并应用这两个函数。

#3


3  

$str = 'base64_decode';
$length = strlen($str);
$result = '';

for ($i = 0; $i < $length; $i++) $result .= '\\x'.str_pad(dechex(ord($str[$i])),2,'0',STR_PAD_LEFT);

print($result);

#4


1  

Here's working code:

工作代码:

function make_hexcodes($text) {
    $retval = '';
    for($i = 0; $i < strlen($text); ++$i) {
        $retval .= '\x'.dechex(ord($text[$i]));
    }

    return $retval;
}

echo make_hexcodes('base64_decode');

See it in action.

看它的实际应用。