UTF-16 to UTF-8 PHP conversion

1.5k views Asked by At

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

3

There are 3 answers

1
IN4 International On BEST ANSWER

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.

0
AmigoJack On

That is UTF-16BE (most significat bit first), not LE (see also UTF-16 examples).

// Hexadecimal text: each 2 characters describe 1 byte
$sText= '007A006E0061006B006F007600690020010D0107017E0020006800610068006100730068';

// Actually forming bytes of that text, i.e. making '7A' a 'z' and '20' a ' '
$sUtf16= pack( 'H*', $sText );

If you really need UTF-8 from that:

// Since we now have an actual encoding: convert it to the wanted one
$sUtf8= mb_convert_encoding( $sUtf16, 'UTF-8', 'UTF-16BE' );

// To make sure the consumer interpretes the data correctly
header( 'Content-type: text/plain; charset=UTF-8' );
echo $sUtf8;

But if the client is able to cope with different encodings anyway (such as an internet browser) you could output UTF-16BE right away:

header( 'Content-type: text/html; charset=UTF-16BE' );
echo $sUtf16;

This code even works with PHP5 and no additional extensions are needed.

0
JosefZ On

The following code snippet could help (I'm a php noob so make a function by yourself; maybe you need to install or enable PHP extension intl):

<?php
$strinput = "007A006E0061006B006F007600690020010D0107017E0020006800610068006100730068";
print "$strinput\n";

$stroutput = '';
for ( $i = 0; $i < strlen($strinput); $i += 4 ) {
    $stroutput .= IntlChar::chr(hexdec( substr( $strinput,$i,4)));
};

print $stroutput;
?>

Output: .\SO\64382302.php

007A006E0061006B006F007600690020010D0107017E0020006800610068006100730068
znakovi čćž hahash