How to convert integer value to array of four bytes in python

后端 未结 7 1910
太阳男子
太阳男子 2020-12-14 06:54

I need to send a message of bytes in Python and I need to convert an unsigned integer number to a byte array. How do you convert an integer value to an array of four bytes i

7条回答
  •  悲&欢浪女
    2020-12-14 07:40

    This is kind of an old thread, but in Python 3.2+ now you can simply say:

    number = 100
    number.to_bytes(4, byteorder = 'big')
    

    or byteorder = 'little' as per your needs. Documentation here.

提交回复
热议问题