I am trying to send bengali text as sms using our local carrier api. But they don't support unicode (utf-8) text as post/get parameter. they replied this:
For every Bengali alphabet there is standard HEXDUMP representation which need to be inserted in message content part.
Like below Bengali word is having below HEXDUMP representation
বাংলাদেশ : 09AC09BE098209B209BE09A609C709B6
So, I tried following two code gathered from SO.
Code-1:
$strBN = 'বাংলাদেশ';
echo bin2hex($strBN);
//it reutrns this value "e0a6ace0a6bee0a682e0a6b2e0a6bee0a6a6e0a787e0a6b6"
Code-2:
$strBN = 'বাংলাদেশ';
echo fToHex($strBN);
function fToHex($string)
{
$strHData = '';
for ($i = 0; $i < strlen($string); $i++)
{
$strHData .= str_pad(dechex(ord($string[$i])), 2, '0', STR_PAD_LEFT);
}
return $strHData;
}
//This also return same value as above "e0a6ace0a6bee0a682e0a6b2e0a6bee0a6a6e0a787e0a6b6"
So, my question is how I can convert that text/string to hexdump as my carrier expected.
The hex dump that you are getting is UTF-8 format, which is a way to represent Unicode characters reliably in a 8-bit stream.
E0 A6 AC E0 A6 BE E0 A6 82 E0 A6 B2 E0 A6 BE E0 A6 A6 E0 A7 87 E0 A6 B6
The example on the other hand is a dump of the UTF-16 (or truncated 16-bit Unicode codepoint) values:
09AC 09BE 0982 09B2 09BE 09A6 09C7 09B6
In your case the solution is to convert to UTF-16 encoding:
echo bin2hex(mb_convert_encoding('বাংলাদেশ', 'UTF-16'));"
> 09ac09be098209b209be09a609c709b6
Note that using Unicode characters in code is unreliable, because the interpretation of the bytes in a string will depend on your system details / editor / compiler or interpreter settings etc.