I’m working with
os.urandom and I was puzzled by this output:
>>> x = os.urandom(16) >>> print(x) b'cCIp>\xfebUe\x02c\xc9\xbf5P\xe2' >>> type(x) bytes >>> len(x) 16 >>> x 254 >>> x.hex() '634349703efe6255650263c9bf3550e2' >>> x # ASCII code for 'c' is 99 (decimal) 99 >>> hex(x) # ASCII code for 'c' is 63 (hexadecimal) '0x63' >>> hex(x) '0xfe'
This question on StackOverflow clears things up greatly.
print(x) displays a “mixed hexadecimal/ascii” representation of the
x. Bytes that correspond to ASCII characters are displayed as ASCII characters, and bytes that do not are displayed in hexadecimal notation. Which is a little bit confusing if you aren’t expecting it.