Author paddy3118
Recipients docs@python, paddy3118
Date 2012-11-29.19:25:09
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1354217109.61.0.534443148437.issue16580@psf.upfronthosting.co.za>
In-reply-to
Content
http://docs.python.org/3.3/library/stdtypes.html?highlight=to_bytes#int.to_bytes and http://docs.python.org/3.3/library/stdtypes.html?highlight=to_bytes#int.to_bytes would benefit from an example showing what they do based on simpler coding.

I have such an example that I wrote here: http://paddy3118.blogspot.co.uk/2012/11/some-identities-for-python-inttobytes.html that you can use.

I.e.

>>> n = 2491969579123783355964723219455906992268673266682165637887
>>> length = 25
>>> n2bytesbig    = n.to_bytes(length, 'big')
>>> n2byteslittle = n.to_bytes(length, 'little')
>>> assert n2bytesbig    == bytes( (n >> i*8) & 0xff for i in reversed(range(length)))
>>> assert n2byteslittle == bytes( (n >> i*8) & 0xff for i in range(length))
>>> assert n == sum( n2bytesbig[::-1][i] << i*8 for i in range(length) )
>>> assert n == sum( n2byteslittle[i]    << i*8 for i in range(length) )
>>> assert n == int.from_bytes(n2bytesbig,    byteorder='big')
>>> assert n == int.from_bytes(n2byteslittle, byteorder='little')
>>>
History
Date User Action Args
2012-11-29 19:25:09paddy3118setrecipients: + paddy3118, docs@python
2012-11-29 19:25:09paddy3118setmessageid: <1354217109.61.0.534443148437.issue16580@psf.upfronthosting.co.za>
2012-11-29 19:25:09paddy3118linkissue16580 messages
2012-11-29 19:25:09paddy3118create