COnverting integer hex format into strings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pumeta2001
    New Member
    • Aug 2010
    • 1

    COnverting integer hex format into strings

    Hi everybody,

    I am programming an application to send data using UDP sockets with Python 3.1.

    The command socket.send requires data in bytes format.

    The problem I am having is that the package I have to send has three different fields, the first one contains a 16 bits integer variable (c_ushort) and so does the second field whereas the third one is an string whose length can go up to 900 characters.

    I decided then to create an struct that contains these three fields:
    Code:
    class PHAL_msg(Structure):
        _fields_ = [("Port", c_ushort),
                    ("Size", c_ushort),
                    ("Text", c_wchar_p)]
    I would expect I could send this object by just converting it to a bytes object:

    Code:
    Msg_TX = PHAL_msg(Port=PHAL_ADDRESS, Size=PAYLOAD_SIZE, Text='HELLO woRLD!')
    
    socket.send(bytes(Msg_TX))
    , but it does not work.

    Any idea how this could be done?

    Regards
    Last edited by bvdet; Aug 31 '10, 01:31 PM. Reason: Add code tags
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Define "doesn't work" ...

    Are you sure your code produces an array of bytes to be sent?
    Are you sure your code has correctly set up the socket?
    How do you receive the data at the destination computer?
    Do you receive nothing or do you receive something but it appears to be wrong?
    Are there and endian issues to be taken into account?
    Are you actually transmitting across a network, as opposed to sending an receiving on the same machine? If you are have you used a utility like WireShark to monitor network traffic while you are trying to send?

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      You also want to print bytes(Msg_TX) and see if it is what you expect.

      Comment

      Working...