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

Hexadecimal Conversion in Python

P: n/a
Hello, I am reading in a socket message from a server and am only
receiving this '....'. Now obviously it is in the wrong format. How
would I convert these bys in Python, I have looked everywhere but I do
not see much documentation on converting ptyhon types to other data
types.
Any Help would be appreciated.

Nov 2 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
DaBeef wrote:
Hello, I am reading in a socket message from a server and am only
receiving this '....'. Now obviously it is in the wrong format. How
would I convert these bys in Python, I have looked everywhere but I do
not see much documentation on converting ptyhon types to other data
types.


What is the server supposed to send (you need to know that
if you want to decode it).
Then, lookup struct.unpack .

HTH

Stefan
Nov 2 '05 #2

P: n/a
it is returning data such as 0x04. I am new to python so this is a
pain for me, learning to do this in a language whose llibrary is
somewhat limited. But instead I receieve .... So I wnat to convert to
the original data. Also can you define a constant in Python such as
#define value 0x04
Thank-you for your help

Nov 2 '05 #3

P: n/a
DaBeef wrote:
it is returning data such as 0x04.
But you need to know what kind of data the other side is sending,
i.e. what kind of protocol it speaks.
I am new to python
New and starting with socket/network programming ?
Brave!
so this is a
pain for me, learning to do this in a language whose llibrary is
somewhat limited.


You mean Pythons' library is limited?
You know nothing...
But instead I receieve .... So I wnat to convert to

the original data. Also can you define a constant in Python such as
#define value 0x04


You should look here:

http://docs.python.org/

And probably here, too:

http://diveintopython.org/toc/index.html
What does your 'socket receiver' look like ?
Stefan
--
Stefan Näwe
stefan_AT_naewe_DOT_de
Nov 2 '05 #4

P: n/a
"DaBeef" wrote:/
it is returning data such as 0x04. I am new to python so this is a
pain for me, learning to do this in a language whose llibrary is
somewhat limited.


you make no sense at all.

what are you receiving data from? how are you receiving it? what
library are you using? what's 0x04? an integer? a string? a byte?
what's .... ? a string? a typo? an odd character that's munged by
your mail program?

</F>

Nov 2 '05 #5

P: n/a
I have been coding for 5 years. This is a proprietary protocol, so it
is difficult converting. I did this in java but was just able to
convert a stream. I looked through the Python library, I am more or
less getting backa string represented as a "...." So now I want to
convert it to all the hexa, bin until I see a match and can then work
teh rest of my program

Nov 2 '05 #6

P: n/a
On 2 Nov 2005 12:28:26 -0800, "DaBeef" <De***********@gmail.com> wrote:
Hello, I am reading in a socket message from a server and am only
receiving this '....'. Now obviously it is in the wrong format. How
would I convert these bys in Python, I have looked everywhere but I do
not see much documentation on converting ptyhon types to other data
types.
Any Help would be appreciated.

print repr(msg)
where msg is what you _actually_ read (and tell us how you got the message in, BTW)
and show us a copy/pasted copy from your screen.
Unless you are very very good at descriptions, it's hard to beat presentation of
machine representations of what you are talking about ;-)

Regards,
Bengt Richter
Nov 2 '05 #7

P: n/a
On 2005-11-02, DaBeef <De***********@gmail.com> wrote:
I have been coding for 5 years. This is a proprietary protocol, so it
is difficult converting.
Eh? What's so difficult about it?
I did this in java but was just able to convert a stream.
Yet you seem unable to describe what it is you're trying to do.
I looked through the Python library, I am more or less getting
backa string represented as a "...."
And what is it you _want_? If the other end sent you four
ASCII "." bytes, shouldn't that be what you see?
So now I want to convert it to all the hexa, bin
Sorry, I've no clue what "hexa, bin" is or how to convert to
it.
until I see a match and can then work teh rest of my program


I have absolutely no idea what you're trying to do, but maybe
this will help: In Python a "string" is an array of 8-bit
bytes.

If you want the integer equivalent of the 3rd byte in a string s,
do this:

b = ord(s[2])

For example:
s = "ABC"
ord(s[0]) 65 ord(s[1]) 66 ord(s[2]) 67
If you want a list of the integer equivalents of the bytes in a
string, do this:

bl = [ord(c) for c in s]
[ord(c) for c in s]

[65, 66, 67]

--
Grant Edwards grante Yow! Make me look like
at LINDA RONSTADT again!!
visi.com
Nov 2 '05 #8

P: n/a
On 2 Nov 2005 12:53:45 -0800, "DaBeef" <De***********@gmail.com> wrote:
I have been coding for 5 years. This is a proprietary protocol, so it
is difficult converting. I did this in java but was just able to
convert a stream. I looked through the Python library, I am more or
less getting backa string represented as a "...." So now I want to
convert it to all the hexa, bin until I see a match and can then work
teh rest of my program

Maybe the binascii module's hexlify will get you into territory more
familiar to you? Python generally stores byte data as type str "strings."
If you want to see the bytes as hex (a string of hex characters ;-) you can e.g.,
import binascii
binascii.hexlify('ABC123...\x01\x02\x03') '4142433132332e2e2e010203'

To convert individual character, you can use a format string on the ordinal value
for c in 'ABC123...\x01\x02\x03': print '%02X'%ord(c), ...
41 42 43 31 32 33 2E 2E 2E 01 02 03

Or perhaps you really want the integer ordinal value itself?
for c in 'ABC123...\x01\x02\x03': print ord(c), ...
65 66 67 49 50 51 46 46 46 1 2 3

(print obviously does a conversion to decimal string representation for output)

If you are interested in the bits, you can check them with bit operations, e.g.,
for c in 'ABC123...\x01\x02\x03':

... print ''.join(chr(48+((ord(c)>>b)&1)) for b in xrange(7,-1,- 1)),
...
01000001 01000010 01000011 00110001 00110010 00110011 00101110 00101110 00101110 00000001 00000010 00000011

(cf. 41 42 42 etc above)

Regards,
Bengt Richter
Nov 2 '05 #9

P: n/a
On Wednesday 02 November 2005 12:53, DaBeef wrote:
I have been coding for 5 years. This is a proprietary protocol, so it
is difficult converting. I did this in java but was just able to
convert a stream. I looked through the Python library, I am more or
less getting backa string represented as a "...." So now I want to
convert it to all the hexa, bin until I see a match and can then work
teh rest of my program


Its great that you are learning python, but it seems you are confused about
how it works, so you are not making sense to a lot of people. Hex conversion
can be done in a lot of ways. You should look into "struct" as others have
suggested, which might be a more resource effecient (less processor cycles)
choice and also give you your data as numerical types. If you have managed to
get to the point of having a string that looks like this:

'\xcb\xdb\xbe\xef'

Then the simplest thing for you to do (least amount of learning) at this point
might be to look at "decode" and "encode"

for example
hex_rep = '\xcb\xdb\xbe\xef'.encode('hex')
print hex_rep

'cbdbbeef'

Also, you might annoy (read: 'you have annoyed') a lot of people by saying
that python has a "llimited" library. Not only are you incorrect, but you
made a typo. You will do well to learn as much of the python library as you
can, but it will take some time. You will also do well to avoid typos and
grammatical errors in your communications.

Also, you need to answer Fredrik's question. Let me restate it. What do you
mean by '....'? This encodes in hex to '2e2e2e2e'. Is this the cipher text
you were expecting? If so, your company may want to re-think its encryption
algorithm.

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Nov 2 '05 #10

P: n/a
DaBeef wrote:
I have been coding for 5 years. This is a proprietary protocol, so it
is difficult converting. I did this in java but was just able to
convert a stream.


What exactly did you do in Java to get the results you want?

Python's library is certainly *not* "limited" in this area, so if you
can describe clearly enough what you want we can certainly help.
Showing an example of Java code that does the job might be a more
successful way of communicating your goals to us.
Nov 2 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.