Robus
Robus

Reputation: 8259

PHP convert unicode to hex

An app wants me to insert strings as text encoded using hex values in proper coding., the encoding being Unicode_No_Compression

For example, for Sześć siedem the correct HEX string is 0053007A0065015B0107002000730069006500640065006D,
Źdźbło = 01790064017A00620142006F
String with no special chars =0053007400720069006E0067002000770069007400680020006E006F0020007300700065006300690061006C002000630068006100720073

I tried playing with MySQL HEX()/UNHEX() and dechex() PHP, but been unable to figure out how to make this conversion. Any ideas?

Upvotes: 4

Views: 6488

Answers (2)

deceze
deceze

Reputation: 522081

You're essentially looking at the hex version of the UCS-2 encoding, I'm guessing. Therefore:

php > echo strtoupper(bin2hex(iconv('UTF-8', 'UCS-2', 'Źdźbło')));
01790064017A00620142006F

Upvotes: 10

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

Try this:

$out = implode("",array_map(function($x) {return sprintf("%04X",ord($x));},str_split($in)));

One liner ;)

Upvotes: 0

Related Questions