By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,710 Members | 1,605 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,710 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Sep 15 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Sep 15 '07 #2

P: n/a
On Sep 15, 1:59 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
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

Sep 15 '07 #3

P: n/a
On Sep 15, 9:55 pm, Boris Dušek <boris.du...@gmail.comwrote:
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

Sep 15 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.