Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP convert unicode to hex

Tags:

php

hex

unicode

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?

like image 393
Robus Avatar asked Dec 27 '22 20:12

Robus


1 Answers

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
like image 174
deceze Avatar answered Dec 29 '22 09:12

deceze