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

John Machin sjmachin at lexicon.net
Sat Sep 15 08:41:31 EDT 2007


On Sep 15, 9:55 pm, Boris Dušek <boris.du... at gmail.com> wrote:
> Hi,
>
> 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.
>
> I thought that something like int('\x01') might work, provided the
> argument is string of length 1, but that throws an error:
>
> >>> int('\x12')
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> ValueError: invalid literal for int():
>
> The code I want to write looks like this:
>
> mem = '\x11\x22\x33\x44'
> factor = 1
> sum = 0
> for byte in mem:
>     sum += int(byte) * factor
>     factor *= 2**8
>
> Could you please tell me how to achieve what I want in Python? (it
> would be straightforward in C)
>

'BlackJack' has already sent you looking for the docs for ord() and
the struct module but a few other clues seem necessary:

1. Don't "think that something like int() might work", read the docs.

2. "factor *= 2 **8"?? Python compiles to bytecode; don't make it work
any harder than it has to. "factor" is quite unnecessary. That loop
needs exactly 1 statement:
    sum = (sum << 8) + ord(byte)

3. Don't use "sum" as a variable name; it will shadow the sum() built-
in function.

4. Read through the docs for *all* the built-in
functions -- all kinds of interesting and useful gadgets to be found
there.

5. In a dark corner there lives a strange beast called the array
module:

>>> import array
>>> array.array('I', '\x01\x00\x00\x00')[0]
1L
>>> array.array('I', '\x00\x00\x00\x01')[0]
16777216L
>>> 2**24
16777216
>>>

HTH,
John




More information about the Python-list mailing list