dongmeng1402 2009-12-09 09:51
浏览 127
已采纳

UCS2 / HexEncoded字符

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 return 06450631062d0628064b06270020063906270644064500200021

How I can get the encoded UCS2 in php?

  • 写回答

2条回答 默认 最新

  • douzhuan4406 2009-12-09 23:27
    关注

    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');
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效