Re: [Tutor] How to convert hex representation of char? (Challenge part 8)

2005-05-19 Thread plust
Kent Johnson wrote:

> 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
> 

Thanks! That solves my problem.

Pieter Lust.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to convert hex representation of char? (Challenge part 8)

2005-05-19 Thread Kent Johnson
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

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to convert hex representation of char? (Challenge part 8)

2005-05-19 Thread Pieter Lust
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.


Pieter Lust

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to convert hex representation of char? (Challenge part 8)

2005-05-18 Thread Alan G
> onepart and anotherpart contain many hex representations of
nonprintable
> characters, like '\x14'. But I can't manage to convert those to the
> actual nonprintable characters. Any hints on how to do this?

'\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.

You can get the numeric value using ord just as you would any other
character:

>>> print ord('\x14')
20
>>> print ord{'Q'}
81


so '\x14' is the character, it should just work...

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauldHTH,


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to convert hex representation of char? (Challenge part 8)

2005-05-18 Thread Terry Carroll
On Wed, 18 May 2005, Pieter Lust wrote:

> onepart and anotherpart contain many hex representations of nonprintable 
> characters, like '\x14'. But I can't manage to convert those to the 
> actual nonprintable characters. Any hints on how to do this?

The string including the \x14 escape should be usable as is.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor