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

前端 未结 4 1297
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 12:07

This is the function I\'m after: -

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

I need big endianness support.

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 12:34

    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]
    

提交回复
热议问题