首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UCS2/十六进制编码字符

UCS2/十六进制编码字符
EN

Stack Overflow用户
提问于 2009-12-09 17:51:54
回答 2查看 9.1K关注 0票数 3

有谁能帮我吗?如何获取UCS2/HexEncoded字符

like 'Hello‘将返回"00480065006C006C006F“

以下是HexEncoded的值:

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

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

我怎样才能在php中获得编码的UCS2?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-12-10 07:27:11

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版本

UTF3.5.5(可以,但是FireFox说我测试的是FireFox -16BE)。

Internet Explorer 7.0 (不正常。未正确检测/转换阿拉伯语。)

代码语言:javascript
复制
<?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版本

FireFox 3.5.5 (100%正常)

Internet Explorer 7.0 (失败。可能必须指定字节顺序。)

代码语言:javascript
复制
<?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%正常)

Internet Explorer 7.0 (100%正常)

代码语言:javascript
复制
<?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');
?>
票数 5
EN

Stack Overflow用户

发布于 2009-12-09 18:31:17

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

引用documentation of the mb_convert_encoding function

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1872773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档