来自iso-8859-1的perl utf8编码返回不同的结果到javascript utf8编码

时间:2012-01-27 10:36:16

标签: javascript perl encoding utf-8

如果我将以下内容从iso-8859-1编码为utf8,则在perl中。

(00000)   0a0b0111 666c6578 74696d65 05427351   ....flextime.BsQ
(00016)   f4aea370 00137469 6d657374 616d7009   ...p..timestamp.
(00032)   05010541 d3c8a8a2 40000004 aff01403   ...A....@.......
(00048)   61090301 090b0104 01040204 03040404   a...............
(00064)   0501                                  ..

结果如下:

(00000)   0a0b0111 666c6578 74696d65 05427351   ....flextime.BsQ
(00016)   c3b4c2ae c2a37000 1374696d 65737461   ......p..timesta
(00032)   6d700905 010541c3 93c388c2 a8c2a240   mp....A........@
(00048)   000004c2 afc3b014 03610903 01090b01   .........a......
(00064)   04010402 04030404 040501              ...........

如果我使用javascript解码perl结果,这就是我得到的:

00000000: 00 0b 00 01 00 11 00 66 00 6c 00 65 00 78 00 74 00 69 00 6d 00 65 00 05 00 42 00 73 00 51 4b a3 |flextimeBsQ.|
00000016: 00 70 00 00 00 13 00 74 00 69 00 6d 00 65 00 73 00 74 00 61 00 6d 00 70 00 09 00 05 00 01 00 05 |ptimestamp.|
00000032: 00 41 04 c8 88 80 00 00 00 00 00 04 fc 14 00 03 00 61 00 09 00 03 00 01 00 09 00 0b 00 01 00 04 |A...a..|
00000048: 00 01 00 04 00 02 00 04 00 03 00 04 00 04 00 04 00 05 00 01 .. .. .. .. .. .. .. .. .. .. .. .. |      |

任何人都可以帮我吗?源是从闪存发送到perl服务器的AMF对象,它将其重新发送到javascript。发送数据的方式(从flash到perl再到javascript)不会改变任何数据。

perl中的代码是:

use Encoding;
from_to($out,"iso-8859-1","utf8",Encode::FB_WARN);

如果我在perl中使用默认的utf8编码,我会得到相同的结果。

这已更新为包含十六进制转储而不是二进制。 我需要一个perl中的encode方法,它将返回与javascript相同的结果。可以进行测试using this page

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

半个回答:

这三个序列再次以PERLQQ表示法。

my $octets =
"\x0a\x0b\x01\x11\x66\x6c\x65\x78\x74\x69\x6d\x65\x05\x42\x73\x51".
"\xf4\xae\xa3\x70\x00\x13\x74\x69\x6d\x65\x73\x74\x61\x6d\x70\x09".
"\x05\x01\x05\x41\xd3\xc8\xa8\xa2\x40\x00\x00\x04\xaf\xf0\x14\x03".
"\x61\x09\x03\x01\x09\x0b\x01\x04\x01\x04\x02\x04\x03\x04\x04\x04".
"\x05\x01";

my $utf8_encoded_octets =
"\x0a\x0b\x01\x11\x66\x6c\x65\x78\x74\x69\x6d\x65\x05\x42\x73\x51".
"\xc3\xb4\xc2\xae\xc2\xa3\x70\x00\x13\x74\x69\x6d\x65\x73\x74\x61".
"\x6d\x70\x09\x05\x01\x05\x41\xc3\x93\xc3\x88\xc2\xa8\xc2\xa2\x40".
"\x00\x00\x04\xc2\xaf\xc3\xb0\x14\x03\x61\x09\x03\x01\x09\x0b\x01".
"\x04\x01\x04\x02\x04\x03\x04\x04\x04\x05\x01";

my $received =
"\x00\x0b\x00\x01\x00\x11\x00\x66\x00\x6c\x00\x65\x00\x78\x00\x74".
"\x00\x69\x00\x6d\x00\x65\x00\x05\x00\x42\x00\x73\x00\x51\x4b\xa3".
"\x00\x70\x00\x00\x00\x13\x00\x74\x00\x69\x00\x6d\x00\x65\x00\x73".
"\x00\x74\x00\x61\x00\x6d\x00\x70\x00\x09\x00\x05\x00\x01\x00\x05".
"\x00\x41\x04\xc8\x88\x80\x00\x00\x00\x00\x00\x04\xfc\x14\x00\x03".
"\x00\x61\x00\x09\x00\x03\x00\x01\x00\x09\x00\x0b\x00\x01\x00\x04".
"\x00\x01\x00\x04\x00\x02\x00\x04\x00\x03\x00\x04\x00\x04\x00\x04".
"\x00\x05\x00\x01";

第三个是 UTF-16BE 编码,00的列是提示。除了第一个八位位组0a丢失外,解码产生类似于第一个八位位组的八位位组序列。其他差异标有*,我没有解释。

Test::HexDifferences::eq_or_dump_diff(
    $octets,
    "\x0a".encode('UTF-8', decode('UTF-16BE', $received))
);

+---+----------------------------+----------------------------+
| Ln|Got                         |Expected                    |
+---+----------------------------+----------------------------+
|  1|0000 : 0A 0B 01 11 : ....   |0000 : 0A 0B 01 11 : ....   |
|  2|0004 : 66 6C 65 78 : flex   |0004 : 66 6C 65 78 : flex   |
|  3|0008 : 74 69 6D 65 : time   |0008 : 74 69 6D 65 : time   |
|  4|000C : 05 42 73 51 : .BsQ   |000C : 05 42 73 51 : .BsQ   |
*  5|0010 : F4 AE A3 70 : ...p   |0010 : E4 AE A3 70 : ...p   *
|  6|0014 : 00 13 74 69 : ..ti   |0014 : 00 13 74 69 : ..ti   |
|  7|0018 : 6D 65 73 74 : mest   |0018 : 6D 65 73 74 : mest   |
|  8|001C : 61 6D 70 09 : amp.   |001C : 61 6D 70 09 : amp.   |
|  9|0020 : 05 01 05 41 : ...A   |0020 : 05 01 05 41 : ...A   |
* 10|0024 : D3 C8 A8 A2 : ....   |0024 : D3 88 E8 A2 : ....   *
* 11|0028 : 40 00 00 04 : @...   |0028 : 80 00 00 04 : ....   *
* 12|002C : AF F0 14 03 : ....   |002C : EF B0 94 03 : ....   *
| 13|0030 : 61 09 03 01 : a...   |0030 : 61 09 03 01 : a...   |
| 14|0034 : 09 0B 01 04 : ....   |0034 : 09 0B 01 04 : ....   |
| 15|0038 : 01 04 02 04 : ....   |0038 : 01 04 02 04 : ....   |
| 16|003C : 03 04 04 04 : ....   |003C : 03 04 04 04 : ....   |
| 17|0040 : 05 01       : ..     |0040 : 05 01       : ..     |
+---+----------------------------+----------------------------+
相关问题