Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Has Python 3 to_bytes been back-ported to python 2.7?

Tags:

python

This is the function I'm after: -

http://docs.python.org/3/library/stdtypes.html#int.to_bytes

I need big endianness support.

like image 307
Jason Avatar asked Apr 15 '13 18:04

Jason


1 Answers

Based on the answer from @nneonneo, here is a function that emulates the to_bytes API:

def to_bytes(n, length, endianess='big'):
    h = '%x' % n
    s = ('0'*(len(h) % 2) + h).zfill(length*2).decode('hex')
    return s if endianess == 'big' else s[::-1]
like image 158
miracle2k Avatar answered Oct 05 '22 08:10

miracle2k