UCS2/HexEncoded 字符

2023-12-11

任何人都可以帮助我吗?我怎样才能获得 UCS2/HexEncoded 字符

像“Hello”将返回“00480065006C006C006F”

这是十六进制编码值:

第0048章 第0065章 006C = l 006C = l 006F=o*

同样在阿拉伯语中(!)将返回 06450631062d0628064b06270020063906270644064500200021

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


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

您需要更改输出的字符集以匹配 UCS-2 才能使用echo将其输出到页面。此外,您可能需要通过设置 Content-Typemeta标题中的标签也是如此。

我在以下 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(好的,但 FireFox 在我的测试中说它是 UTF-16BE。)
Internet Explorer 7.0(不好。无法正确检测/转换阿拉伯语。)

<?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(失败。可能必须指定字节顺序。)

<?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% 正常)

<?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');
?>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UCS2/HexEncoded 字符 的相关文章

随机推荐