Python 3 Building an array of bytes

后端 未结 6 752
终归单人心
终归单人心 2020-12-29 01:44

I need to build a tcp frame with raw binary data, but all examples and tutorials I\'ve found talking about bytes always involve conversion from a string, and that\'s not wha

6条回答
  •  太阳男子
    2020-12-29 02:16

    what about simply constructing your frame from a standard list ?

    frame = bytes([0xA2,0x01,0x02,0x03,0x04])
    

    the bytes() constructor can build a byte frame from an iterable containing int values. an iterable is anything which implements the iterator protocol: an list, an iterator, an iterable object like what is returned by range()...

提交回复
热议问题