On Sun, Aug 8, 2010 at 01:05, Alan Gauld <alan.ga...@btinternet.com> wrote: > > "Richard D. Moores" <rdmoo...@gmail.com> wrote > >> So if os.urandom() had been written so that it printed only hex, >> b'l\xbb\xae\xb7\x0ft' would have been >> >> b'\x6c\xbb\xae\xb7\x0f\x74' , right? > > Yes except that its not urandomthat is printing those values. > urandom returns a string of bytes. > Its the Python interpreter calling the repr() function on > those bytes that is deciding to print either hex or character. > Try this: > >>>> '\x6c\xbb\xae\xb7\x0f\x74' > > 'l\xbb\xae\xb7\x0ft' >>>> >>>> print '\x6c\xbb\xae\xb7\x0f\x74' > > l╗«À☼t >>>> > > In the first case its the repr() of the string that gets > printed in the second its the str(). Its the conversion > function that determines what gets printed not urandom()
Ah. Here are some more (from Python 2.6): >>> s = os.urandom(6);s;print s 'Y\xce\x01\xc6\xd3\xe7' Y╬☺╞╙τ >>> s = os.urandom(6);s;print s '\xd2$\xa1\x03B\xba' ╥$í♥B║ >>> s = os.urandom(6);s;print s '\xc6\x93C\xca\xc3\x18' ╞ôC╩├↑ >>> s = os.urandom(6);s;print s '`DTzL\x98' `DTzLÿ >> How were we supposed to know that all the hexes have 2 digits? How did >> you? > > Simple arithmetic... > Its the hex representation of a byte. A byte is 8 bits long. > A hex digit represents 4 bits (0000-1111) so you need 2 hex digits to > represent 8 bits or one byte. Got it. Thanks again. And to Steven as well. Dick _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor