|
楼主 |
发表于 2019-3-26 13:38:35
|
显示全部楼层
只有第一个print被执行 执行结果如下:
电脑 unicode : 0x7535 0x8111
Traceback (most recent call last):
File "ex16.py", line 14, in <module>
print (b , ' utf8 : ' , hexbytes(b))
File "ex16.py", line 2, in hexbytes
ret = '0x%02x' % ord(s[0])
TypeError: ord() expected string of length 1, but int found
修改后代码如下:
- def hexbytes(s):
- ret = '0x%02x' % ord(s[0])
- for i in range(1 , len(s)):
- ret += ' 0x%02x' % ord(s[i])
- return ret
- a = chr(0x7535) + chr(0x8111)
- b = a.encode("utf8")
- c = a.encode("gbk")
- d = a.encode("gb2312")
- e = b.decode("utf8")
- print (a , 'unicode : ' , hexbytes(a))
- print (b , ' utf8 : ' , hexbytes(b))
- print (c , ' gbk : ' , hexbytes(c))
- print (d , ' gb2312 : ' , hexbytes(d))
- print (e , 'default : ' , hexbytes(e))
复制代码 |
|