Converting 2bit hex representation to integer ?

Fredrik Lundh fredrik at pythonware.com
Wed Oct 19 12:41:07 EDT 2005


Madhusudan Singh 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.

that's two digits, not two bits.

    >>> print int("64", 16)
    100

>> 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() ?

since Python uses string objects to hold binary data, that shouldn't be
necessary.

(struct also supports the CPython low-level buffer protocol, so it works
with any type that sees itself as a string of bytes).

</F> 






More information about the Python-list mailing list