472,353 Members | 1,891 Online

# convert binary data to int

Hello,
I need to convert a 3 byte binary string like
"\x41\x00\x00" to 3 int values ( (65,0,0) in this case).
The string might contain characters not escaped with a \x, like
"A\x00\x00"
Any ideas?
Daniel

Jan 10 '07 #1
5 7490
ru**********@web.de wrote:
I need to convert a 3 byte binary string like
"\x41\x00\x00" to 3 int values ( (65,0,0) in this case).
The string might contain characters not escaped with a \x, like
"A\x00\x00"
>>[ord(c) for c in "A\x00\x41"]
[65, 0, 65]

For more complex conversions look into the struct module.

Peter
Jan 10 '07 #2
ru**********@web.de a écrit :
Hello,
I need to convert a 3 byte binary string like
"\x41\x00\x00" to 3 int values ( (65,0,0) in this case).
The string might contain characters not escaped with a \x, like
"A\x00\x00"
Any ideas?
>>s = "\x41\x00\x00"
[ ord(c) for c in s ]
[65, 0, 0]
Jan 10 '07 #3
At Wednesday 10/1/2007 07:17, ru**********@web.de wrote:
>I need to convert a 3 byte binary string like
"\x41\x00\x00" to 3 int values ( (65,0,0) in this case).
The string might contain characters not escaped with a \x, like
"A\x00\x00"
py[ord(x) for x in "\x41\x00\x00"]
[65, 0, 0]
py[ord(x) for x in "A\x00\x00"]
[65, 0, 0]
py"\x41\x00\x00" == "A\x00\x00"
True
py"\x41\x00\x00" is "A\x00\x00"
True

(The last test is actually irrelevant, however)
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Jan 10 '07 #4
[ord(x) for ...]
perfect, thank you
Daniel

Jan 10 '07 #5
in some cases struct.unpack would also help

ru**********@web.de wrote:
Hello,
I need to convert a 3 byte binary string like
"\x41\x00\x00" to 3 int values ( (65,0,0) in this case).
The string might contain characters not escaped with a \x, like
"A\x00\x00"
Any ideas?
Daniel
Jan 10 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.