Hexadecimal Conversion in Python

Discussion in 'Python' started by Bengt Richter, Nov 2, 2005.

  1. 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
     
    Bengt Richter, Nov 2, 2005
    #1
    1. Advertisements

  2. Bengt Richter

    DaBeef Guest

    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.
     
    DaBeef, Nov 2, 2005
    #2
    1. Advertisements

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

    HTH

    Stefan
     
    Stefan =?UTF-8?B?TsOkd2U=?=, Nov 2, 2005
    #3
  4. Bengt Richter

    DaBeef Guest

    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
     
    DaBeef, Nov 2, 2005
    #4
  5. 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
     
    Bengt Richter, Nov 2, 2005
    #5
  6. :/
    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>
     
    Fredrik Lundh, Nov 2, 2005
    #6
  7. But you need to know what kind of data the other side is sending,
    i.e. what kind of protocol it speaks.
    New and starting with socket/network programming ?
    Brave!
    You mean Pythons' library is limited?
    You know nothing...
    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 =?UTF-8?B?TsOkd2U=?=, Nov 2, 2005
    #7
  8. Bengt Richter

    DaBeef Guest

    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
     
    DaBeef, Nov 2, 2005
    #8
  9. Eh? What's so difficult about it?
    Yet you seem unable to describe what it is you're trying to do.
    And what is it you _want_? If the other end sent you four
    ASCII "." bytes, shouldn't that be what you see?
    Sorry, I've no clue what "hexa, bin" is or how to convert to
    it.
    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:
    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]
     
    Grant Edwards, Nov 2, 2005
    #9
  10. Bengt Richter

    James Stroud Guest

    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/
     
    James Stroud, Nov 2, 2005
    #10
  11. Bengt Richter

    Peter Hansen Guest

    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.
     
    Peter Hansen, Nov 2, 2005
    #11
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.