Hexadecimal Conversion in Python

B

Bengt Richter

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
 
D

DaBeef

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

Stefan =?UTF-8?B?TsOkd2U=?=

DaBeef said:
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
 
D

DaBeef

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
 
B

Bengt Richter

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.,
'4142433132332e2e2e010203'

To convert individual character, you can use a format string on the ordinal value
...
41 42 43 31 32 33 2E 2E 2E 01 02 03

Or perhaps you really want the integer ordinal value itself?
...
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.,
... 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
 
F

Fredrik Lundh

:/
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>
 
S

Stefan =?UTF-8?B?TsOkd2U=?=

DaBeef said:
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...
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
 
D

DaBeef

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
 
G

Grant Edwards

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]
[65, 66, 67]
 
J

James Stroud

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
'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/
 
P

Peter Hansen

DaBeef said:
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.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top