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

前端 未结 4 1294
没有蜡笔的小新
没有蜡笔的小新 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:11

    You can probably use struct.pack instead:

    >>> import struct
    >>> struct.pack('>i', 123)
    '\x00\x00\x00{'
    

    It doesn't do arbitrary lengths in the way int.to_bytes does, but I doubt you need that.

提交回复
热议问题