469,323 Members | 1,615 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

Converting 2bit hex representation to integer ?

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
6 3363
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
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
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
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
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
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.

Similar topics

reply views Thread by Mark Dufour | last post: by
5 posts views Thread by nickisme | last post: by
1 post views Thread by Madhusudan Singh | last post: by
10 posts views Thread by Mantorok Redgormor | last post: by
13 posts views Thread by Paraic Gallagher | last post: by
2 posts views Thread by Alex Buell | last post: by
116 posts views Thread by Dilip | last post: by
5 posts views Thread by Hemant Shah | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.