简体   繁体   中英

os.urandom and byte representation

I have a very basic question about the output format of os.urandom in python2, and byte representation more generally. A byte is 8 bits, so for example in binary a particular single byte would be:

10011001

os.urandom(n) returns a random n-byte string suitable for cryptographic use. If I print out a handful calls to os.urandom(1) , generating just a single byte, I find things like:

'\x1f', '\xa0', '#', '\xa4', "'", '\xa8', '+', '\xac', '/', '\xb0', '3', '\xb4', '7', '\xb8', ';', '\xbc', '?', '\xc0', 'C', '\xc4', 'G', '\xc8', 'K', '\xcc', 'O', '\xd0', 'S', '\xd4', 'W', '\xd8', '['

So it seems that urandom() is representing a byte not as 8 bits but as 2-digit hex numbers (both make sense of course because 2^8 = 16^2). My confusion is that only some of the above are 2-digit hex numbers, there are also characters like 'S' or '?' or '['. What is the output format? What is the rule for converting an output like '[' into binary?

It's really got nothing to with os.urandom - it's just how repr() acts on all strings. Like so:

>>> for ch in "\x1f\xa0#+\x01":
...     print("%6s %3d" % (repr(ch), ord(ch)))
'\x1f'  31
'\xa0' 160
   '#'  35
   '+'  43
'\x01'   1

In general, if a byte corresponds to a printable ASCII character, repr shows that character, else a hex \\x escape is produced.

To see the bytes clearly in Python 2, follow the pattern above, iterating over the characters/bytes and using ord() to reveal their decimal values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM