syaofox
syaofox

Reputation: 31

Convert 10 to bytes, strange result

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'?

Upvotes: 3

Views: 777

Answers (1)

Dan
Dan

Reputation: 670

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.

Upvotes: 2

Related Questions