Author alexandre.vassalotti
Recipients alexandre.vassalotti, bob.ippolito, josiahcarlson, loewis, mark.dickinson, mwh, pitrou, rhettinger, tim.peters
Date 2009-08-11.21:38:06
SpamBayes Score 4.05147e-07
Marked as misclassified No
Message-id <1250026691.07.0.0626354502326.issue1023290@psf.upfronthosting.co.za>
In-reply-to
Content
I went ahead and coded a new API for converting long integers to byte
arrays and vice-versa. My patch adds two new methods to the long type:
.as_bytes() and .frombytes(). The patch itself is well-documented; but
nevertheless, here's some examples: 

>>> (1024).as_bytes()
b'\x04\x00'
>>> (1024).as_bytes(fixed_length=10)
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> (-1024).as_bytes(fixed_length=10)
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> (-1024).as_bytes(little_endian=True)
b'\x00\xfc'
>>> ((2**16)-1).as_bytes(fixed_length=2, signed=False)
b'\xff\xff'

>>> int.frombytes(b'\x00\x10')
16
>>> int.frombytes(b'\x00\x10', little_endian=True)
4096
>>> int.frombytes(b'\xfc\x00')
-1024
>>> int.frombytes(b'\xfc\x00', signed=False)
64512


This patch depends on another patch posted in issue #6687. So, apply the
other patch before testing this one.
History
Date User Action Args
2009-08-11 21:38:11alexandre.vassalottisetrecipients: + alexandre.vassalotti, mwh, tim.peters, loewis, rhettinger, josiahcarlson, bob.ippolito, mark.dickinson, pitrou
2009-08-11 21:38:11alexandre.vassalottisetmessageid: <1250026691.07.0.0626354502326.issue1023290@psf.upfronthosting.co.za>
2009-08-11 21:38:09alexandre.vassalottilinkissue1023290 messages
2009-08-11 21:38:09alexandre.vassalotticreate