By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,675 Members | 2,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,675 IT Pros & Developers. It's quick & easy.

Converting 2bit hex representation to integer ?

P: n/a
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.
Oct 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

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

Oct 19 '05 #2

P: n/a
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() ?
Oct 19 '05 #3

P: n/a
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() ?


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 ?
Oct 19 '05 #4

P: n/a
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>

Oct 19 '05 #5

P: n/a
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() ?

Oct 20 '05 #6

P: n/a
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
Oct 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.