[Tutor] How to convert hex representation of char? (Challenge part 8)
Kent Johnson
kent37 at tds.net
Thu May 19 11:52:24 CEST 2005
Pieter Lust wrote:
> Alan G wrote:
> > '\x14' is the actual non printable charactewrs. If it were printable
> > you
> > would see its printed representation, because it isn't Pyhon showsw
> > you
> > the hex code as an escaped character but it is the character.
> >
>
> Thanks for the input. I'm sorry, my question was not clear enough.
>
> The problem is that (to stick with the example) '\x14' is not the
> character 0x14, but a string of length 4. It consists of the characters
> backslash, 'x', '1' and '4'. (I verified the output of len()).
> What I want to do is make Python believe that that string of length 4 is
> actually a string of length 1 that contains character 0x14. Any help on
> how to achieve that is appreciated.
One way is to paste the string into a program; when Python sees '\x14' it creates a string of length 1:
>>> len('\x14')
1
Alternately you can use the 'string_escape' codec to convert the string:
>>> s=r'\x14'
>>> len(s)
4
>>> t=s.decode('string_escape')
>>> t
'\x14'
>>> len(t)
1
Kent
More information about the Tutor
mailing list