python2与python3在包含十六进制的字符串中的比较

2024-06-16 12:11:20 发布

您现在位置:Python中文网/ 问答频道 /正文

考虑一下,使用python 3.4

% python3
Python 3.4.2
% echo `python3 -c "print('a' * 72 + '\xff\xbe\xbf\xff')"` | hexdump -x
0000000    6161    6161    6161    6161    6161    6161    6161    6161
*
0000040    6161    6161    6161    6161    bfc3    bec2    bfc2    bfc3
0000050    000a                                                        
0000051

这个,用python 2.7.9

% python2 --version
Python 2.7.9
% echo `python2 -c "print('a' * 72 + '\xff\xbe\xbf\xff')"` | hexdump -x
0000000    6161    6161    6161    6161    6161    6161    6161    6161
*
0000040    6161    6161    6161    6161    beff    ffbf    000a        
000004d

这真的是来自python 3.4实现的bug吗?你知道吗


Tags: echoversionhexdumppython3printxffpython2考虑一下
1条回答
网友
1楼 · 发布于 2024-06-16 12:11:20

python2的纯'带引号的字符串表示字节字符串;python3表示字符字符串。相反语言中的等价物分别是bytesb'literal')和unicodeu'literal')。你知道吗

% python3 -c "from sys import stdout; stdout.buffer.write(b'a' * 72 + b'\xff\xbe\xbf\xff\n')" | hexdump -x

相关问题 更多 >