3

任何人都可以帮助我吗?我怎么能得到 UCS2/HexEncoded 字符

像“你好”将返回“00480065006C006C006F”

这是 HexEncoded 值:

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

同样在阿拉伯语中 (!مرحبا عالم) 将返回 06450631062d0628064b06270020063906270644064500200021

如何在 php 中获取编码的 UCS2?

4

2 回答 2

5

mb_convert_encoding($str, 'UCS-2', 'auto')可以正确转换字符串,但您必须做额外的工作才能在浏览器中获得正确的输出。

您需要更改输出的字符集以匹配 UCS-2,以便能够使用echo将其输出到页面。此外,您可能还需要通过标题中的标记设置 Content-Type。

我在以下 unicode 变体中包含了三个示例:UCS-2、UTF-16 和 UTF-8;因为并非所有这些都为我工作而无需在 Internet Explorer 中进行调整。您可能需要以 UTF-8 格式存储 PHP 文件以获得正确的结果。另外,我使用的是英文版的 Windows,所以我无法以正确的 RTL 形式输入你的阿拉伯字符串。如果您的字符串在这里出现乱码,我很抱歉。我向您保证,如果您在我的评论指出的位置替换它,您将获得正确的结果。最后,您可能无法在 Internet Explorer 中查看 UCS-2 和 UTF-16 - 当通过缓存重新加载输出时似乎有些奇怪。但是,FireFox 3.5.5 适用于所有三种编码。如果您认真地制作应用程序,我强烈建议您考虑使用 UTF-8 而不是 UCS-2。

UCS-2 版本

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');
?>
于 2009-12-09T23:27:11.027 回答
0

根据this web page,多字节字符串模块(mbstring)支持UCS-2。启用此模块后,您可以使用函数mb_convert_encoding将字符串从一种编码转换为另一种编码。

引用mb_convert_encoding 函数的文档

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 . 
于 2009-12-09T10:31:16.777 回答