Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28207

Re: how to get character hex number?

From Peter Otten <__peter__@web.de>
Subject Re: how to get character hex number?
Date 2012-09-01 08:37 +0200
Organization None
References <CA+YdQ_66ap1tQCRibdkntGvHqE1sp+Nv_kK2iiKh50GoAa=K6Q@mail.gmail.com> <50417631.1080508@tim.thechases.com> <CA+YdQ_6s1dj_ewZR0wT-WAd1uCwR8dhDDusbnkot4p5DkgmE5w@mail.gmail.com> <50418609.8030902@tim.thechases.com>
Newsgroups comp.lang.python
Message-ID <mailman.27.1346481430.27098.python-list@python.org> (permalink)

Show all headers | View raw


Tim Chase wrote:

> On 08/31/12 22:41, contro opinion wrote:
>>>>>>> u"english".encode("utf-8")
>>>> 'english'
>>>>>>> u"english".encode("ascii")
>>>> 'english'
>>>>
>>>> how can i get 656e676c697368 in encode method?
>>>
>>> At least in 2.x, you can do:
>>>
>>>  >>> u"english".encode("hex")
>>>  '656e676c697368'
>>
>> how about in python3.0?
> 
> Well, in 3.1.3 at least, using the u"..." notation dies on me with
> an invalid syntax.  However, as Ian suggests, you can do
> 
>   my_str = "english"
>   "".join("%02x" % c for c in my_str.encode("ascii"))
> 
> or whatever other encoding you want instead of "ascii".

Another option:

>>> binascii.hexlify("english".encode()).decode()
'656e676c697368'

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: how to get character hex number? Peter Otten <__peter__@web.de> - 2012-09-01 08:37 +0200

csiph-web