int('\x23') != 0x23 (a.k.a convert char to integer of its byte representation)

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sat Sep 15 13:59:26 CEST 2007


On Sat, 15 Sep 2007 11:55:28 +0000, Boris Dušek wrote:

> I am looking for the best way to convert a string of length 1 (= 1
> character as string) to integer that has the same value as numeric
> representation of that character. Background: I am writing functions
> abstracting endianness, e.g. converting a string of length 4 to the
> appropriate integer value (e.g. '\x01\x00\x00\x00' = 2**24 for big
> endian memory, 2**0 for little endian memory). For this, I need to
> know the numeric value of each byte and sum them according to
> endianness.

So you are looking for the `struct` module in the standard library instead
of doing this yourself.  :-)

If you insist on doing it yourself take a look at the built-in `ord()`
function.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list