Converting 2bit hex representation to integer ?

Peter Hansen peter at engcorp.com
Fri Oct 21 09:50:36 EDT 2005


Madhusudan Singh wrote:
> I just tried 
> 
> n=str(x)
> print struct.unpack("b",n)
> 
> I get (51,)
> 
> What is the deal with the parenthesis
> and the comma ?

If you really don't know what the parentheses and comma mean in the 
above output, I would suggest that you need to go back a step and walk 
through the Python tutorial before you try to continue on with what you 
are attempting to do.  (Section 5.3 specifically covers your question.) 
  Either that, or read the documentation on struct.unpack() since that 
describes what it returns fairly clearly.

Assuming your input represents a C-style integer (i.e. fixed size) 
rather than a Python long integer (of arbitrary length), you should be 
able to use just struct.unpack('i', mybinarydata)[0] to get the result 
you need...

-Peter



More information about the Python-list mailing list