PHP utf 编码问题

发布于 2024-08-31 15:14:31 字数 126 浏览 4 评论 0原文

如何在 PHP 中对 UTF-16BE 格式的字符串进行编码?对于“演示消息!!!”编码字符串应为“00440065006D006F0020004D00650073007300610067006”。另外,我需要将阿拉伯字符编码为这种格式。

How can I encode strings on UTF-16BE format in PHP? For "Demo Message!!!" the encoded string should be '00440065006D006F0020004D00650073007300610067006'. Also, I need to encode Arabic characters to this format.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

当爱已成负担 2024-09-07 15:14:31

首先,这绝对不是UTF-8,它只是一个字符集(即在内存中存储字符串/显示字符串的一种方式)

这里的内容看起来像是用于构建每个字符的字节的转储。

如果是这样,您可以通过这种方式获取这些字节:

$str = utf8_encode("Demo Message!!!");

for ($i=0 ; $i<strlen($str) ; $i++) {
    $byte = $str[$i];
    $char = ord($byte);
    printf('%02x ', $char);
}

并且您将获得以下输出:

44 65 6d 6f 20 4d 65 73 73 61 67 65 21 21 21 

But, once again, this is not UTF-8 : in UTF-8, like you can see in the example I've give, `D` is stored on only one byte : `0x44`

在您发布的内容中,它是使用两个字节存储的:0x00 0x44

也许您正在使用某种 UTF-16 ?


经过更多测试和 @aSeptik 的评论后进行编辑:这确实是 UTF-16。

要获得您所获得的转储类型,您必须确保您的字符串以 UTF-16 编码,这可以通过这种方式完成,例如使用 mb_convert_encoding 函数:

$str = mb_convert_encoding("Demo Message!!!", 'UTF-16', 'UTF-8');

然后,它是只需迭代构成此字符串的字节并转储它们的值,就像我之前所做的那样:

for ($i=0 ; $i<strlen($str) ; $i++) {
    $byte = $str[$i];
    $char = ord($byte);
    printf('%02x ', $char);
}

您将得到以下输出:

00 44 00 65 00 6d 00 6f 00 20 00 4d 00 65 00 73 00 73 00 61 00 67 00 65 00 21 00 21 00 21 

哪种看起来像您发布的内容:-)

(您只需删除对 printf 调用中的空格——我将其放在那里以获得更易于阅读的输出=)

First of all, this is absolutly not UTF-8, which is just a charset (i.e. a way to store strings in memory / display them).

WHat you have here looks like a dump of the bytes that are used to build each characters.

If so, you could get those bytes this way :

$str = utf8_encode("Demo Message!!!");

for ($i=0 ; $i<strlen($str) ; $i++) {
    $byte = $str[$i];
    $char = ord($byte);
    printf('%02x ', $char);
}

And you'd get the following output :

44 65 6d 6f 20 4d 65 73 73 61 67 65 21 21 21 

But, once again, this is not UTF-8 : in UTF-8, like you can see in the example I've give, `D` is stored on only one byte : `0x44`

In what you posted, it's stored using two Bytes : 0x00 0x44.

Maybe you're using some kind of UTF-16 ?


EDIT after a bit more testing and @aSeptik's comment : this is indeed UTF-16.

To get the kind of dump you're getting, you'll have to make sure your string is encoded in UTF-16, which could be done this way, using, for example, the mb_convert_encoding function :

$str = mb_convert_encoding("Demo Message!!!", 'UTF-16', 'UTF-8');

Then, it's just a matter of iterating over the bytes that make this string, and dumping their values, like I did before :

for ($i=0 ; $i<strlen($str) ; $i++) {
    $byte = $str[$i];
    $char = ord($byte);
    printf('%02x ', $char);
}

And you'll get the following output :

00 44 00 65 00 6d 00 6f 00 20 00 4d 00 65 00 73 00 73 00 61 00 67 00 65 00 21 00 21 00 21 

Which kind of looks like what youy posted :-)

(you just have to remove the space in the call to printf -- I let it there to get an easier to read output=)

江心雾 2024-09-07 15:14:31

例如,通过使用 mbstring 扩展及其 mb_convert_encoding() 函数。

$in = 'Demo Message!!!';
$out = mb_convert_encoding($in, 'UTF-16BE');

for($i=0; $i<strlen($out); $i++) {
  printf("%02X ", ord($out[$i]));
}

或者

00 44 00 65 00 6D 00 6F 00 20 00 4D 00 65 00 73 00 73 00 61 00 67 00 65 00 21 00 21 00 21 

使用 iconv()

$in = 'Demo Message!!!';
$out = iconv('iso-8859-1', 'UTF-16BE', $in);

for($i=0; $i<strlen($out); $i++) {
  printf("%02X ", ord($out[$i]));
}

E.g. by using the mbstring extension and its mb_convert_encoding() function.

$in = 'Demo Message!!!';
$out = mb_convert_encoding($in, 'UTF-16BE');

for($i=0; $i<strlen($out); $i++) {
  printf("%02X ", ord($out[$i]));
}

prints

00 44 00 65 00 6D 00 6F 00 20 00 4D 00 65 00 73 00 73 00 61 00 67 00 65 00 21 00 21 00 21 

Or by using iconv()

$in = 'Demo Message!!!';
$out = iconv('iso-8859-1', 'UTF-16BE', $in);

for($i=0; $i<strlen($out); $i++) {
  printf("%02X ", ord($out[$i]));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文