Byte Array in Python

后端 未结 4 1065
小蘑菇
小蘑菇 2020-12-04 15:23

How can I represent a byte array (like in Java with byte[]) in Python? I\'ll need to send it over the wire with gevent.

byte key[] = {0x13, 0x00, 0x00, 0x00,         


        
4条回答
  •  一向
    一向 (楼主)
    2020-12-04 15:35

    An alternative that also has the added benefit of easily logging its output:

    hexs = "13 00 00 00 08 00"
    logging.debug(hexs)
    key = bytearray.fromhex(hexs)
    

    allows you to do easy substitutions like so:

    hexs = "13 00 00 00 08 {:02X}".format(someByte)
    logging.debug(hexs)
    key = bytearray.fromhex(hexs)
    

提交回复
热议问题