Hexadecimal Conversion in Python

Grant Edwards grante at visi.com
Wed Nov 2 16:17:23 EST 2005


On 2005-11-02, DaBeef <DerrickHaller at 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            



More information about the Python-list mailing list