开发者

UCS2/HexEncoded characters

Any one can help me ? how could I get UCS2/HexEncoded characters

like 'Hello' will return "00480065006C006C006F"

This are the HexEncoded values:

0048 = H 0065 = e 006C = l 006C = l 006F = o*

Also in arabic (!مرحبا عالم) will r开发者_C百科eturn 06450631062d0628064b06270020063906270644064500200021

How I can get the encoded UCS2 in php?


mb_convert_encoding($str, 'UCS-2', 'auto') works correctly to convert the string, but you'll have to do extra work to get the proper output in a browser.

You'll need to change the character set of your output to match UCS-2 in order to be able to use echo to output it to a page. Also, you may need to set the Content-Type via a meta tag in the header as well.

I've included three examples here in the following unicode variants: UCS-2, UTF-16, and UTF-8; as not all of them worked for me without tweaking in Internet Explorer. You may need to store your PHP files in UTF-8 to get proper results. Also, I am on an english version of Windows, so I can't enter your arabic string in proper RTL form. I'm sorry if your string is garbled here. I assure you that if you replace it in the location noted by my comments, you'll get the proper result. Finally, you may have trouble viewing UCS-2 and UTF-16 in internet explorer- there seems to be some oddities when the output is reloaded via a cache. However, FireFox 3.5.5 worked for all three encodings. If you're serious about making an app, I strongly recommend you consider using UTF-8 instead of UCS-2.

UCS-2 Version

FireFox 3.5.5 (Ok, but FireFox says it is UTF-16BE on my test.)
Internet Explorer 7.0 (Not Ok. Didn't detect/convert Arabic properly.)

<?php
header('Content-Type: text/html; charset=UCS-2');
mb_http_output('UCS-2');
echo mb_convert_encoding('<html><head><meta http-equiv="Content-Type" content="text/html; charset=UCS-2" /></head><body>', 'UCS-2', 'auto');
echo mb_convert_encoding('encoding: ', 'UCS-2', 'auto');
echo mb_convert_encoding(mb_http_output(), 'UCS-2', 'auto');
echo mb_convert_encoding('<br />', 'UCS-2', 'auto');
// NOTE: Replace the string here with your phrase
$strTerm = '!مرحبا عالم';
echo mb_convert_encoding('$strTerm = '.$strTerm.'<br />', 'UCS-2', 'auto');
echo mb_convert_encoding('query string: '.$_SERVER['QUERY_STRING'].'<br />', 'UCS-2', 'auto');
echo mb_convert_encoding('original hex: '.bin2hex($strTerm).'<br />', 'UCS-2', 'auto');
echo mb_convert_encoding('transformed hex: '.bin2hex(mb_convert_encoding($strTerm, 'UCS-2', 'auto')).'<br />', 'UCS-2', 'auto');
echo mb_convert_encoding('</body>', 'UCS-2', 'auto');
?>

UTF-16 Version

FireFox 3.5.5 (100% Ok)
Internet Explorer 7.0 (Fail. May have to specify Byte-Order.)

<?php
header('Content-Type: text/html; charset=UTF-16');
mb_http_output('UTF-16');
echo mb_convert_encoding('<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-16" /></head><body>', 'UTF-16', 'auto');
echo mb_convert_encoding('encoding: ', 'UTF-16', 'auto');
echo mb_convert_encoding(mb_http_output(), 'UTF-16', 'auto');
echo mb_convert_encoding('<br />', 'UTF-16', 'auto');
// NOTE: Replace the string here with your phrase
$strTerm = '!مرحبا عالم';
echo mb_convert_encoding('$strTerm = '.$strTerm.'<br />', 'UTF-16', 'auto');
echo mb_convert_encoding('query string: '.$_SERVER['QUERY_STRING'].'<br />', 'UTF-16', 'auto');
echo mb_convert_encoding('original hex: '.bin2hex($strTerm).'<br />', 'UTF-16', 'auto');
echo mb_convert_encoding('transformed hex: '.bin2hex(mb_convert_encoding($strTerm, 'UTF-16', 'auto')).'<br />', 'UTF-16', 'auto');
echo mb_convert_encoding('</body>', 'UTF-16', 'auto');
?>

UTF-8

FireFox 3.5.5 (100% Ok)
Internet Explorer 7.0 (100% Ok)

<?php
header('Content-Type: text/html; charset=UTF-8');
mb_http_output('UTF-8');
echo mb_convert_encoding('<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>', 'UTF-8', 'auto');
echo mb_convert_encoding('encoding: ', 'UTF-8', 'auto');
echo mb_convert_encoding(mb_http_output(), 'UTF-8', 'auto');
echo mb_convert_encoding('<br />', 'UTF-8', 'auto');
// NOTE: Replace the string here with your phrase
$strTerm = '!مرحبا عالم';
echo mb_convert_encoding('$strTerm = '.$strTerm.'<br />', 'UTF-8', 'auto');
echo mb_convert_encoding('query string: '.$_SERVER['QUERY_STRING'].'<br />', 'UTF-8', 'auto');
echo mb_convert_encoding('original hex: '.bin2hex($strTerm).'<br />', 'UTF-8', 'auto');
echo mb_convert_encoding('transformed hex: '.bin2hex(mb_convert_encoding($strTerm, 'UTF-8', 'auto')).'<br />', 'UTF-8', 'auto');
echo mb_convert_encoding('</body>', 'UTF-8', 'auto');
?>


According to this web page, the Multi-Byte String module (mbstring) supports UCS-2. After enabling this module, you can use the function mb_convert_encoding to convert a string from one encoding to the other.

Quoting the documentation of the mb_convert_encoding function:

string mb_convert_encoding  ( string $str  , string $to_encoding  [, mixed $from_encoding  ] )
Converts the character encoding of string str to to_encoding from optionally from_encoding . 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜