Convert binary information to regular data type without outside modules in python.

Posted by vgm64 on Stack Overflow See other posts from Stack Overflow or by vgm64
Published on 2010-03-28T00:14:16Z Indexed on 2010/03/28 0:23 UTC
Read the original article Hit count: 621

Filed under:
|
|
|

Hello World. I'm tasked with reading a poorly formatted binary file and taking in the variables. Although I need to do it in C++ (ROOT, specifically), I've decided to do it in python because python makes sense to me, but my plan is to get it working in python and then tackle re-writing in in C++, so using easy to use python modules won't get me too far later down the road.

Basically, I do this:

In [5]: some_value
Out[5]: '\x00I'

In [6]: ''.join([str(ord(i)) for i in some_value])
Out[6]: '073'

In [7]: int(''.join([str(ord(i)) for i in some_value]))
Out[7]: 73

And I know there has to be a better way. What do you think?

© Stack Overflow or respective owner

Related posts about python

Related posts about binary