Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert text to \x codes?

Tags:

php

encode

hex

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.

like image 859
Vinay Jeurkar Avatar asked Sep 06 '11 13:09

Vinay Jeurkar


2 Answers

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

like image 57
sanmai Avatar answered Oct 20 '22 00:10

sanmai


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.

like image 39
Evert Avatar answered Oct 19 '22 22:10

Evert