只是寻找可以将所有字符从普通字符串(所有英语字母字母)转换为python中的ascii hex的python代码.我不确定我是否以错误的方式问这个问题,因为我一直在寻找这个,但似乎无法找到这个.
我必须完成答案,但我会喜欢一些帮助.
只是澄清一下,从'地狱'到'\ x48\x65\x6c\x6c'
小智 6
根据 Jon Clements 的回答,尝试使用 python3.7 上的代码。我有这样的错误:
>>> s = '1234'
>>> hexlify(s)
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
hexlify(s)
TypeError: a bytes-like object is required, not 'str'
Run Code Online (Sandbox Code Playgroud)
通过以下代码解决:
>>> str = '1234'.encode()
>>> hexlify(str).decode()
'31323334'
Run Code Online (Sandbox Code Playgroud)
我想''.join(r'\x{02:x}'.format(ord(c)) for c in mystring)会做的伎俩......
>>> mystring = "Hello World"
>>> print ''.join(r'\x{02:x}'.format(ord(c)) for c in mystring)
\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64
Run Code Online (Sandbox Code Playgroud)
就像是:
>>> s = '123456'
>>> from binascii import hexlify
>>> hexlify(s)
'313233343536'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7130 次 |
| 最近记录: |