hexadecimal string to byte array in python

前端 未结 8 1820
孤城傲影
孤城傲影 2020-11-22 11:53

I have a long Hex string that represents a series of values of different types. I wish to convert this Hex String into a byte array so that I can shift each value out and co

相关标签:
8条回答
  • 2020-11-22 12:49

    There is a built-in function in bytearray that does what you intend.

    bytearray.fromhex("de ad be ef 00")
    

    It returns a bytearray and it reads hex strings with or without space separator.

    0 讨论(0)
  • 2020-11-22 12:52

    A good one liner is:

    byte_list = map(ord, hex_string)
    

    This will iterate over each char in the string and run it through the ord() function. Only tested on python 2.6, not too sure about 3.0+.

    -Josh

    0 讨论(0)
提交回复
热议问题