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

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

    And for completeness: you can also use the array module:

    >>> from array import array
    >>> a = array('I', [100]) # note that 'I' and such are machine-dependent.
    >>> a.tostring()
    '\d\x00\x00\x00'
    >>> a.byteswap()
    >>> a.tostring()
    '\x00\x00\x00\d'
    

提交回复
热议问题