Vinay Jeurkar
Vinay Jeurkar

Reputation: 3132

How to convert text to \x codes?

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.

Upvotes: 5

Views: 9527

Answers (6)

Will B.
Will B.

Reputation: 18436

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.

Hexadecimal Encoding: https://3v4l.org/Ai3HZ

bin2hex

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

unpack

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

sprintf

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

Result

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

Hexadecimal Decoding

To decode the encoded string back to the plain-text, use one of the following methods.

$encoded = '\x62\x61\x73\x65\x36\x34\x5f\x64\x65\x63\x6f\x64\x65';
$hexadecimal = str_replace('\x', '', $encoded);

hex2bin

echo hex2bin($hexadecimal);

pack

echo pack('H*', $hexadecimal);

sscanf + vprintf

vprintf(str_repeat('%c', count($f = sscanf($hexadecimal, str_repeat('%02X', substr_count($encoded , '\x'))))), $f);

Result

base64_decode

Upvotes: 1

Netwons
Netwons

Reputation: 1648

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);
    }

Upvotes: 0

Evert
Evert

Reputation: 99851

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.

Upvotes: 5

sanmai
sanmai

Reputation: 30951

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

Upvotes: 6

DaveRandom
DaveRandom

Reputation: 88697

$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);

Upvotes: 3

Jon
Jon

Reputation: 437904

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.

Upvotes: 1

Related Questions