Converting 2bit hex representation to integer ?
Larry Bates
larry.bates at websafe.com
Thu Oct 20 10:04:20 EDT 2005
No you can't convert using str(). Binary data is
stored in a Python string object, but it isn't really
a string. It is rather just a bunch of bits packed
into a string variable. struct.unpack() will unpack
those bits into any number of different types of
variables and is what you need.
Example:
import struct
s='\x64'
values=struct.unpack('b',s)
print "values=",values
value=(100,)
Note: struct.unpack returns a tuple of values. Just get
values[0] to get the first one.
Larry
Madhusudan Singh wrote:
> Larry Bates wrote:
>
>
>>Can you give us an example. I don't know what two bit
>>hex means (takes at least 4 bits to make a hex digit).
>
>
> Like 64(base 16)=100.
> I am referring to 64 in the above.
>
>
>>Now I'm going to try to guess:
>>
>>If the data is binary then all you need to do is to
>>use the struct.unpack module to convert to integer.
>
>
> Doesn't unpack presume that the input is a string ? If so, is it safe to
> convert binary data to string using str() ?
More information about the Python-list
mailing list