Search code examples
phputf-8utf-16le

UTF-16 to UTF-8 PHP conversion


i've a GSM-7 returned string from USB modem device that contains UTF-16 encoded string. Example string is "007A006E0061006B006F007600690020010D0107017E0020006800610068006100730068".

I need a PHP solution (function) to convert string from UTF-16 (little endian) to UTF-8 (human readable format). Translation from above string should be this "znakovi čćž hahash". I've spend few hours looking for appropriate solution but without success. I've tried to use iconv and mb_convert_encoding with a lot of different options but i haven't got desired result. I've found an online service to convert string and here is the print screen https://prnt.sc/v09r57

Thank you in advance


Solution

  • i've already found simpler solution few days ago that works perfectly. If someone needs use:

        $string="007A006E0061006B006F007600690020010D0107017E0020006800610068006100730068";
        $packed = pack('H*', $string);
        echo iconv("UTF-16BE","UTF-8",$packed);
    

    Thank you all for response.