Python 3.1.1 string to hex

前端 未结 9 916
野的像风
野的像风 2020-11-28 04:59

I am trying to use str.encode() but I get

>>> \"hello\".encode(hex)
Traceback (most recent call last):
  File \"\", line 1         


        
9条回答
  •  春和景丽
    2020-11-28 05:40

    The easiest way to do it in Python 3.5 and higher is:

    >>> 'halo'.encode().hex()
    '68616c6f'
    

    If you manually enter a string into a Python Interpreter using the utf-8 characters, you can do it even faster by typing b before the string:

    >>> b'halo'.hex()
    '68616c6f'
    

    Equivalent in Python 2.x:

    >>> 'halo'.encode('hex')
    '68616c6f'
    

提交回复
热议问题