Converting a string of one char into the ASCII equivalent?

Tim Peters tim_one at email.msn.com
Mon May 10 21:56:07 EDT 1999


[Sean Hummel]
> I've been looking for a good way to convert a 1 length string 
> into a signed byte equivalent.
> ...

>>> char = '\300'
>>> ord(char) << 24 >> 24              # assumes 32-bit ints
-64
>>> ord(char) - (ord(char) >> 7 << 8)  # works everywhere
-64
>>> import array
>>> array.array('b', char)[0]          # the most straightforward
-64
>>>

quickly y'rs  - tim






More information about the Python-list mailing list