Converting 2bit hex representation to integer ?

M

Madhusudan Singh

Hi

I am using binascii.b2a_hex to convert some binary data to hex. The result
is a two bit hex representation (i. e., without the leading hex). How do I
convert the resulting two bit representation into an integer ?

Thanks.
 
L

Larry Bates

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).

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.

Larry Bates
 
M

Madhusudan Singh

Larry said:
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() ?
 
M

Madhusudan Singh

Madhusudan said:
Like 64(base 16)=100.
I am referring to 64 in the above.


Doesn't unpack presume that the input is a string ? If so, is it safe to
convert binary data to string using str() ?

Followup to this :

I just tried :

n=str(x)
print struct.unpack("b",n)

I get (51,)

The corresponding output of binascii.b2a_hex(x) is 33.

And 51_10=33_16. So that checks out. What is the deal with the parenthesis
and the comma ?
 
F

Fredrik Lundh

Madhusudan said:
Like 64(base 16)=100.
I am referring to 64 in the above.

that's two digits, not two bits.
100
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>
 
L

Larry Bates

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
 
P

Peter Hansen

Madhusudan said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top