[Tutor] os.urandom()

Dave Angel davea at ieee.org
Mon Aug 9 11:23:56 CEST 2010


Richard D. Moores wrote:
> On Sun, Aug 8, 2010 at 08:11, bob gailer <bgailer at gmail.com> wrote:
>   
>> On 8/8/2010 1:57 AM, Richard D. Moores wrote:
>>     
>
>   
>>> How were we supposed to know that all the hexes have 2 digits?
>>>       
>> In version 2.6.5 Language Reference 2.4.1 - String literals:
>> \xhh Character with hex value hh
>>     
>
> But
>   
>>>> os.urandom(6)
>>>>         
> b'\x13\xf1\x138a\xcc'
>
> In my Active Python 3.1 docs, Language Ref 2.4.1. String and Bytes
> literals, I have
>
> The same as what you quoted, plus a couple of footnotes:
> \xhh Character with hex value hh (2,3)
>
> That footnote 2, "Unlike in Standard C, at most two hex digits are
> accepted.", seems necessary; otherwise that \x138a could be seen by my
> former, byte-ignorant self as a \xhhhh, and puzzled about why I got
> only 4 bytes, not 6.  :)
>
> But I never would have thought to have looked in the docs where you did.
>
> Dick
>
>   
Big difference between 2.x and 3.x.  In 3.x, strings are Unicode, and 
may be stored either in 16bit or 32bit form (Windows usually compiled 
using the former, and Linux the latter).

Presumably in 3.x, urandom returns a byte string   (see the b'xxxx' 
form), which is 8 bits each, same as 2.x strings.  So you'd expect only 
two hex digits for each character.

DaveA



More information about the Tutor mailing list