5

I want to convert normal text to \x codes for e.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.

Vinay Jeurkar
  • 3,044
  • 9
  • 36
  • 55

6 Answers6

6

PHP 5.3 one-liner:

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

sanmai
  • 26,245
  • 12
  • 59
  • 74
  • not working for my special string: `base64_decode('OVUP/PZrLzoZpaeQga/TpK9xeJAOuTTbUzm9NplDuhSUMkklPtDeDQDgidUEIX+ijxe80EI')` – Minqi Pan Aug 19 '12 at 05:39
  • It works just fine on my computer with that string. I could not see why it can't work unless you have PHP version older that 5.3. – sanmai Aug 22 '12 at 10:34
  • Sorry my fault, it's good, with results slightly different from @DaveRandom's. – Minqi Pan Aug 23 '12 at 00:59
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.

sepehr
  • 15,475
  • 7
  • 79
  • 114
Evert
  • 83,661
  • 18
  • 106
  • 170
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);
DaveRandom
  • 86,228
  • 11
  • 149
  • 173
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.

Jon
  • 413,451
  • 75
  • 717
  • 787
  • @DaveRandom: Why pad? Also, the [string parsing rules](http://www.php.net/manual/en/language.types.string.php) don't require escaping in this particular case. You can see for yourself by clicking on the live example link or reading the docs. – Jon Sep 06 '11 at 13:26
  • because any character with a value of less than 16 will result in, e.g. `\xa` rather than `\x0a`. Thinking about it, I don't know if this matters, I have always just assumed it does. And escape your backslash because it needs escaping. In a single quoted string it does matter so much, but it's still good practice. – DaveRandom Sep 06 '11 at 13:29
  • 1
    @DaveRandom: Sorry, I have to disagree. The documentation is pretty clear that padding is not required and that the slash in `'\x'` does not need escaping. – Jon Sep 06 '11 at 13:30
  • The page you linked do says explicitly, in the 'Single Quoted' section: `To specify a literal backslash, double it (\\)`. PHP is pretty good at working out what you meant, but since you can still use \ to specify a literal single quote, you technically should escape the backslash. – DaveRandom Sep 06 '11 at 13:31
  • 1
    @DaveRandom: And it continues: *All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning*. I don't think it's a matter of "working out what you want": those are *documented rules*. – Jon Sep 06 '11 at 13:32
  • Granted, and I do accept your point and that it will work without it. I think the manual should be clearer really, since the only time it would matter is if you wanted a literal `\'` in a single quoted string. – DaveRandom Sep 06 '11 at 13:34
0

For an alternative to dechex(ord()) you can also use bin2hex($char), sprintf('\x%02X') or unpack('H*', $char). Additionally instead of using preg_replace_callback, you can use array_map with str_split.

echo implode(array_map(function($char) {
    return '\x' . bin2hex($char);
}, (array) str_split($word)));
echo implode(array_map(function($char) {
    return '\x' . implode(unpack('H*', $char));
}, (array) str_split($word)));
echo implode(array_map(function($char) {
    return sprintf('\x%02X', ord($char));
}, (array) str_split($word)));

Example: https://3v4l.org/6Pc6X

bin2hex

echo implode(array_map(function($char) {
    return '\x' . bin2hex($char);
}, (array) str_split('base64_decode')));

Result

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

unpack

echo implode(array_map(function($char) {
    return '\x' . implode(unpack('H*', $char));
}, (array) str_split('base64_decode')));

Result

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

sprintf

echo implode(array_map(function($char) {
    return sprintf('\x%02X', ord($char));
}, (array) str_split('base64_decode')));

Result

\x62\x61\x73\x65\x36\x34\x5f\x64\x65\x63\x6f\x64\x65
Will B.
  • 15,971
  • 4
  • 61
  • 66
0

im not read this code \ud83d\udc33

function unicode_decode(string $str)
    {
       str="Learn Docker in 12 Minutes \ud83d\udc33"
        return preg_replace_callback('/u([0-9a-f]{4})/i', function ($match) {
            return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
        }, $str);
    }
Netwons
  • 700
  • 9
  • 12