Convert 10 to bytes, strange result

只谈情不闲聊 提交于 2021-02-10 06:06:05

问题


i = 11
print(i.to_bytes(1,'big'))

b'\x0b'

i = 10
print(i.to_bytes(1,'big'))

b'\n'

It should be b'\x0a'

Why i got b'\n'?


回答1:


Well, as khelwood also explained in his comment, at roughly the same time, here's the deal:

As the newline character has a "c-style shorthand", namely \n, and also a numerical value of 0x0a, or 10 in decimal, these representations are equivalent. It's only that Python decides to show you the traditional escape sequence.

To be fair, character 11, or 0x0b is the vertical tab, so it could be represented as \v, but apparently Python does not do that one.

Please see https://en.wikipedia.org/wiki/Control_character#In_ASCII and http://www.asciitable.com/ for details.



来源:https://stackoverflow.com/questions/42899909/convert-10-to-bytes-strange-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!