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

G

Guest

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:
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
 
M

Marc 'BlackJack' Rintsch

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
 
G

Guest

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
 
J

John Machin

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:


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top