Converting a string of one char into the ASCII equivalent?

Michael P. Reilly arcege at shore.net
Mon May 10 21:31:56 EDT 1999


Sean Hummel <seanh at unforgettable.com> wrote:
: I've been looking for a good way to convert a 1 length string into a signed
: byte equivalent.  Unfortunately the string library functions for converting
: a string into a number, of course only work with string representations of a
: number.  Thus if the string I have was "\300" then the atoi(), long(), and
: int() functions all complain that the string isn't a number.

: Is there a function which will give me the signed byte equivalent of the 1
: char string?

It's not going to be a signed byte (not defined by ASCII or Latin-1).

>>> ord('a')
97
>>> chr(98)
'b'
>>> ord('\300')
192
>>>

There is a SIG conserned with larger string-related issues tho: other
character sets, wide characters, etc.
  (http://www.python.org/sigs/string-sig/)

  -Arcege





More information about the Python-list mailing list