function _UTF8toUTF16($s)
{
// Convert UTF-8 to UTF-16BE with BOM
$res = "þÿ";
$nb = strlen($s);
$i = 0;
while ($i < $nb) {
$c1 = ord($s[$i++]);
if ($c1 >= 224) {
// 3-byte character
$c2 = ord($s[$i++]);
$c3 = ord($s[$i++]);
$res .= chr((($c1 & 0xf) << 4) + (($c2 & 0x3c) >> 2));
$res .= chr((($c2 & 0x3) << 6) + ($c3 & 0x3f));
} elseif ($c1 >= 192) {
// 2-byte character
$c2 = ord($s[$i++]);
$res .= chr(($c1 & 0x1c) >> 2);
$res .= chr((($c1 & 0x3) << 6) + ($c2 & 0x3f));
} else {
// Single-byte character
$res .= "" . chr($c1);
}
}
return $res;
}