Question

I'm using Python 2.7.6 ctypes struct:

class TestStruct(Structure):
   _fields_=[
          ("ValueA",c_int),
          ("StringA",c_char*9)
          ]

And I get datas from udp like this:

    sock = socket.socket(socket.AF_INET,
                         socket.SOCK_DGRAM)
    sock.bind((self.__ip,self.__port))
    print("Starting Receiving......")
    while True:
        try:
            (data,addr) = sock.recvfrom(1024)
            t = cast( data, POINTER(TestStruct))
            print t.ValueA
            #print(data[0])
            #print(type(data))
        except Exception,ex:
            print ex
            break

As you can see , I'm using "cast", but what it converted is the one whose type looks like:"main.LP_TestStruct".

I wanna get is TestStruct object , what function should I use ? Thanks!

Was it helpful?

Solution

You can do t.contents to get the object the pointer points to (TestStruct). But I imagine you'd be even better off using Python's struct module for this (as @eryksun mentioned).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top