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

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

=?utf-8?B?Qm9yaXMgRHXFoWVr?=
Guest
Posts: n/a

 09-15-2007
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)

Thanks for any suggestions,
Boris Dušek

Marc 'BlackJack' Rintsch
Guest
Posts: n/a

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

=?utf-8?B?Qm9yaXMgRHXFoWVr?=
Guest
Posts: n/a

 09-15-2007
On Sep 15, 1:59 pm, Marc 'BlackJack' Rintsch <(E-Mail Removed)> wrote:
> 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.
>

Thanks Marc,

both things are exactly what I was looking for (I am a bit ashamed
that I did not find the built-in ord

Boris

John Machin
Guest
Posts: n/a

 09-15-2007
On Sep 15, 9:55 pm, Boris Dušek <(E-Mail Removed)> 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 << + 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