How to convert text to \x codes?

后端 未结 6 1484
别跟我提以往
别跟我提以往 2021-01-06 02:27

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 =         


        
相关标签:
6条回答
  • 2021-01-06 02:43

    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
    
    0 讨论(0)
  • 2021-01-06 02:47
    $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);
    
    0 讨论(0)
  • 2021-01-06 02:53

    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.

    0 讨论(0)
  • 2021-01-06 02:57

    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

    0 讨论(0)
  • 2021-01-06 02:57

    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.

    0 讨论(0)
  • 2021-01-06 03:03

    im not read this code \ud83d\udc33

    0 讨论(0)
提交回复
热议问题