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

后端 未结 7 1909
太阳男子
太阳男子 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:53

    Sven has you answer. However, byte shifting numbers (as in your question) is also possible in Python:

    >>> [hex(0x12345678 >> i & 0xff) for i in (24,16,8,0)]
    ['0x12', '0x34', '0x56', '0x78']
    

提交回复
热议问题