473,383 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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 3602
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mark Dufour | last post by:
Hi all, I need to convert an integer into some binary representation. I found the following code in the online cookbook (adapted to return a list): binary = lambda n: n>0 and +binary(n>>1) or ...
5
by: nickisme | last post by:
Hi - sorry for the possibly stupid question, but I'm still a wee starter on c++... Just wondering if there's a quick way to convert data into binary strings... To explain, I'm trying to convert...
25
by: TK | last post by:
I'm used to programming in c or c++ in which my problem is simple. I want to be able to enter a value on a page (like 3.2), and then read it as a 32-bit float and break it into it's individual...
1
by: Madhusudan Singh | last post by:
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 0x). How do I convert the resulting two bit...
10
by: Mantorok Redgormor | last post by:
I always see posts that involve the representation of integers, where some poster claims that the unerlyding representation of an integer doesn't have to reflect on the actual integer, for example:...
13
by: Paraic Gallagher | last post by:
Hi, This is my first post to the list, I hope somebody can help me with this problem. Apologies if it has been posted before but I have been internet searching to no avail. What I am trying...
2
by: Alex Buell | last post by:
Is there an elegant way of converting strings containing digits between different number bases in C++? I.e.: 10 (base 2) = 2 (base 10) FF (base 16) = 256 (base 10) F (base 16) = 1111 (base 2)...
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
5
by: Hemant Shah | last post by:
Folks, How can I convert date/time/timestamp to an integer? According to UDB 8.1 docs that I have, INTEGER('1964-07-20') should return 19640720, but when I run the SQL statement I get...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.