Python3 数字 chr 后拼接成字符串和其他语言有区别?

2018-04-17 11:36:27 +08:00
 assad

items = [119,156,161,138,90,240,56,165,114,4]

python3

    while i < strlenth:
        self.result += chr(item)
        i += 1

php

    while ($i < $strlenth) {
        $this->result .= chr($item);
        $i++;

结果拼出来的字符不一样,MD5 和 base64 均不一样

2040 次点击
所在节点    程序员
3 条回复
chenstack
2018-04-17 12:16:05 +08:00
要说区别可能是 python3 字符串 encode 成 bytes 后不一样,因为 encode 默认编码是 utf-8,所给的测试代码并不完整。

python3:
from hashlib import md5
items = [119,156,161,138,90,240,56,165,114,4]
strlenth = len(items)

result = ''
i = 0
while i < strlenth:
result += chr(items[i])
i += 1
print(md5(result.encode('latin-1')).hexdigest())


php:
<?php

$items = [119,156,161,138,90,240,56,165,114,4];
$strlenth = count($items);

$i = 0;
$result = '';
while ($i < $strlenth) {
$result .= chr($items[$i]);
$i++;
}
print(md5($result));
?>

结果均为 0e996abf46e4d5acadead68fbf1f877e
assad
2018-04-17 13:05:47 +08:00
encode('latin-1'),真奇葩,居然是 latin-1,我试过各种编码,就是没用这个
@chenstack
chenstack
2018-04-17 13:30:20 +08:00
百科的介绍:Latin1 是 ISO-8859-1 的别名,有些环境下写作 Latin-1。ISO-8859-1 编码是单字节编码,向下兼容 ASCII,其编码范围是 0x00-0xFF。因为 ISO-8859-1 编码范围使用了单字节内的所有空间,在支持 ISO-8859-1 的系统中传输和存储其他任何编码的字节流都不会被抛弃。
所以用 encode('latin-1')时所以字节会原封不动的保留,不会被转换。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/447441

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX