Python 3.1.1 string to hex

前端 未结 9 934
野的像风
野的像风 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:39

    You've already got some good answers, but I thought you might be interested in a bit of the background too.

    Firstly you're missing the quotes. It should be:

    "hello".encode("hex")
    

    Secondly this codec hasn't been ported to Python 3.1. See here. It seems that they haven't yet decided whether or not these codecs should be included in Python 3 or implemented in a different way.

    If you look at the diff file attached to that bug you can see the proposed method of implementing it:

    import binascii
    output = binascii.b2a_hex(input)
    

提交回复
热议问题