Native PHP function for hex string representation?

Solution:1

There is no native function, but you can use hex2bin to eval PHP code which is in hex, e.g.

eval(hex2bin("6563686f20706928293b")); # Output: 3.14159265359
eval(hex2bin(bin2hex("echo pi();")));  # Behind the scenes.

Or you can call the function which name is in hex string:

$ echo '<?$_=hex2bin(7069);die($_());' | php # 7069 = pi
3.14159265359

Solution:2

There is no built in PHP function to achieve the same results, but with some PHP-Fu you can do it in one line:

$str = 'ABC';
$str = '\x'.implode('\x', str_split(bin2hex($str), 2));
echo $str; // \x41\x42\x43

There is also a piece of code in the PHP docs which gives exactly the same results:

$str = 'ABC';
$field=bin2hex($str);
$field=chunk_split($field,2,"\\x");
$field= "\\x" . substr($field,0,-2);
echo $field; // \x41\x42\x43