469,365 Members | 1,788 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,365 developers. It's quick & easy.

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 7345
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Hako | last post: by
2 posts views Thread by Lucas Cowald | last post: by
8 posts views Thread by John A Grandy | last post: by
6 posts views Thread by as400tips | last post: by
7 posts views Thread by elliotng.ee | last post: by
5 posts views Thread by =?Utf-8?B?YmJkb2J1ZGR5?= | last post: by
6 posts views Thread by Bob Altman | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.