Convert list of byte strings to bytearray (byte stream)

后端 未结 3 1728
Happy的楠姐
Happy的楠姐 2021-01-07 04:58

I have a list of hex strings representing bytes, of the form \"FF\". I want to convert the whole list to a byte stream so I can send it over a socket (Python 3). It looks

3条回答
  •  独厮守ぢ
    2021-01-07 06:02

    hexlist = ["a9", "00", "85", "c6"]
    ba = bytearray(h.decode("hex") for h in hexlist)
    

    See also bytearray.fromhex:

    bytearray.fromhex(string) -> bytearray

    Create a bytearray object from a string of hexadecimal numbers. Spaces between two numbers are accepted. Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\xb9\x01\xef').

提交回复
热议问题