Bob0101
Bob0101

Reputation: 187

How to decode hex content?

I have $_SERVER['REDIRECT_SSL_CLIENT_S_DN'] content that has somekind of hex data. How can i decode it?

$_SERVER['REDIRECT_SSL_CLIENT_S_DN'] = '../CN=\x00M\x00\xC4\x00,\x00I\x00S\x00,\x004\x000\x003\x001\x002\x000\x000\x002/SN=..';
$pattern = '/CN=(.*)\\/SN=/';
preg_match($pattern, $_SERVER['REDIRECT_SSL_CLIENT_S_DN'], $server_matches);
print_r($server_matches[1]);

The result is:

\x00M\x00\xC4\x00,\x00I\x00S\x00,\x004\x000\x003\x001\x002\x000\x000\x002

The result i need is:

MÄ,IS,40312002

I tried to decode it with chr(hexdec($value)); and it almost works, but in html input i see lot of question marks.

EDIT: Additional test with results. Not yet perfect. Array reveals some errors: http://pastebin.com/BC4xxqmE

Upvotes: 2

Views: 942

Answers (3)

gen_Eric
gen_Eric

Reputation: 227180

After using utf8_encode, you now have a multibyte string. This means you need to use PHP's multibyte (mb_) functions.

So, str_split won't work anymore. You need to use either mb_split or preg_split with the u flag.

$splitted = preg_split('//u', $string);

Here's a demo showing that your code is now working: http://ideone.com/nqeC0U

Upvotes: 1

Du D.
Du D.

Reputation: 5300

Have you tried unicode equivalent of chr()? chr mod 256 all the input that's why you see all those question marks.

The code below is from one of the post in chr php manual

function unichr($u) {
    return mb_convert_encoding('&#' . intval($u) . ';', 'UTF-8', 'HTML-ENTITIES');
}

Update

//New function
function unichr($intval) {
return mb_convert_encoding(pack('n', $intval), 'UTF-8', 'UTF-16BE');
}

I test with xC4=196 it gives me an Ä

http://codepad.viper-7.com/3htuwW

Upvotes: 1

Dustin D
Dustin D

Reputation: 15

Your input is in UTF-8 using that conversion is similar to utf8_decode which will convert to ISO-8859-1. UTF-8 though supports more characters than ISO-8859-1. This is why xC4 shows up as a question mark for you.

Try using something more powerful like iconv.

Upvotes: 0

Related Questions