Question

I am passing my message to SMS api,

This is the documentation

Normally Unicode Messages are Arabic and Chinese Message, which are defined by GSM Standards. Unicode messages are nothing but normal text type messages but it has to be submitted in HEX form. To submit Unicode messages following Url to be used.

I tried bin2hex() there is not working for the output.

$str = '人';
//$str = 'a';

$output = bin2hex($str);
echo $output; 

//output
//人 = e4baba ; I would expect '4EBA'

I found a similar solution but it is in VB.net anyone can convert it? http://www.supportchain.com/index.php?/Knowledgebase/Article/View/28/7/unable-to-send-sms-with-chinese-character-using-api

the sample i had tried, and it is work:-

example of conversion : a converted to hexadecimal is 0061, 人 converted to hexadecimal is 4EBA

Was it helpful?

Solution

The issue you are facing has to do with encoding. Since these are considered special characters, you need to add some encoding details when converting to hex.

Each of these outputs exactly what you were looking for when I run them:

echo bin2hex(iconv('UTF-8', 'ISO-10646-UCS-2', '人')) . PHP_EOL;
//Outputs 4eba

echo bin2hex(iconv('UTF-8', 'UNICODE-1-1', '人')) . PHP_EOL;
//Outputs 4eba

echo bin2hex(iconv('UTF-8', 'UTF-16BE', '人')) . PHP_EOL;
//Outputs 4eba

Pick whichever one you fancy.

If you want to convert back:

echo iconv('UTF-16BE', 'UTF-8', hex2bin('4eba')) . PHP_EOL;
//outputs 人
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top