hex string to character in python

后端 未结 4 1157
心在旅途
心在旅途 2021-01-01 20:37

I have a hex string like:

data = \"437c2123\"

I want to convert this string to a sequence of characters according to the ASCII table. The

4条回答
  •  走了就别回头了
    2021-01-01 21:03

    In [17]: data = "437c2123"
    
    In [18]: ''.join(chr(int(data[i:i+2], 16)) for i in range(0, len(data), 2))
    Out[18]: 'C|!#'
    

    Here:

    • for i in range(0, len(data), 2) iterates over every second position in data: 0, 2, 4 etc.
    • data[i:i+2] looks at every pair of hex digits '43', '7c', etc.
    • chr(int(..., 16)) converts the pair of hex digits into the corresponding character.
    • ''.join(...) merges the characters into a single string.

提交回复
热议问题