2

Is there a native PHP function for converting a string to its HEX representation that can be then eval()'d as a string, such as:

"ABC" => "\x41\x42\x43"

I know it can be done in several steps, I'm just wondering if I'm unnecessarily complicating something that could be done with a single, native function?

3
  • 2
    The closest you can get is with bin2hex(), then you'd have to add the \x to each character manually: '\x' . implode( '\x', array_map( 'bin2hex', str_split( 'ABC')))); Commented Apr 14, 2013 at 17:02
  • What do you mean by eval()'d if it is a string? Commented Apr 14, 2013 at 17:02
  • @arkascha I mean that "\x41\x42\x43" is valid PHP, and could be eval'd. Commented Apr 14, 2013 at 17:47

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

4 Comments

I think you misunderstood the original question!
Hex representation that can be then eval'd as a string? So can you clarify the question?
So the question is about 'function for converting', not how to eval a string? I think I do now.
I agree my question is somewhat confusing now that I read it, I meant something like var_export() but exporting the values as hex only!
0

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

Comments

0
$n = 1234;
echo printf("%x", $n); //should return the hexadecimal format of the number

2 Comments

First off, printf() prints directly to output, so you don't need to echo its return value. Secondly, this is no way answers the question. OP is dealing with strings, not integers.
Yep, this is not answering the question!
0

Why don't you try something like this?

function strtohex($string) {
    if (!empty($string)) {
        $output = null;
        $count = strlen($string, "UTF-8");

        for ($i = 0; $i < $count; $i++) {
            $output .= dechex(ord($string[$i]));
        }

        return $output;
    }
}

For one-liners, the only alternative I can think about is the bin2hex() function, which is native in PHP and it offers the same.

2 Comments

I never said that my string was UTF-8, it can actually be binary! Also, your function does not return the format asked in the question. Finally, there's a bug in your code: if your string is UTF-8, $string[$i] will only return one byte. To retrieve one character from a UTF-8 string, you need to use mb_substr().
Yes, I've just noticed it right now. I'm accustomed to write multibyte-aware code and I didn't checked this snippet well enough. Sorry about it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.