[Python-Dev] os.urandom API

Josiah Carlson jcarlson at uci.edu
Mon Aug 30 05:48:49 CEST 2004


(Quick apology to Tim for accidentally sending this to him only, I 
recently switched email clients and am still getting used to it)

> Note that it's easy (abeit obscure) to generate a long from a string s:
> 
>     long(binascii.hexlify(s), 16)

or even:
     long(s.encode('hex'), 16)


> It's harder to go in the other direction.  First you do hex(n). Then

Perhaps what is needed is a method to easily convert between large 
integers and strings.  It seems as though a new struct conversion code 
is in order, something that works similarly to the way the 's' code works:

#pack the integer bigint as a signed big-endian packed binary string,
#null-filling as necessary, for 64 bytes of precision
a = struct.pack('>64g', bigint)

#unpack an unsigned little-endian packed binary string of 24 bytes to a 
#python long
b = struct.unpack('<24G', a)


With such a change, I think many of the string/integer translation 
complaints would disappear.

  - Josiah



More information about the Python-Dev mailing list