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

convert binary data to int

P: n/a
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
Share this Question
Share on Google+
5 Replies


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

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

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

P: n/a
[ord(x) for ...]
perfect, thank you
Daniel

Jan 10 '07 #5

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.