在python中删除字符串中的\ x

Eva*_*Lin 2 python


我需要通过python获得little endian随机字符串.
如'0010'为256(不带'\ x')
这是我的代码.

 import random
 import struct
 str1 = struct.pack('<Q', random.randint(1, 1000))
 #Ex: str1 = '\xc9\x02\x00\x00\x00\x00\x00\x00'
Run Code Online (Sandbox Code Playgroud)

但我不知道将此字符串转换为"C902000000000000"请给出任何建议,谢谢

glg*_*lgl 6

另一种非常容易输入的替代方法是以下方法:

>>> import random
>>> import struct
>>> str1 = struct.pack('<Q', random.randint(1, 1000))
>>> str1.encode('hex')
Run Code Online (Sandbox Code Playgroud)