I want to encode a message…This is the message that i have generated
from ctypes import memmove, addressof, Structure, c_uint16,c_bool
class AC(Structure):
_fields_ = [("UARFCN", c_uint16),
("ValidUARFCN", c_bool ),("PassiveActivationTime", c_uint16) ]
def __init__(self , UARFCN ,ValidUARFCN , PassiveActivationTime):
self.UARFCN = UARFCN
self.ValidUARFCN = True
self.PassiveActivationTime = PassiveActivationTime
def __str__(self):
s = "AC"
s += "UARFCN:" + str(self.UARFCN)
s += "ValidUARFCN" + str(self.ValidUARFCN)
s += "PassiveActivationTime" +str(self.PassiveActivationTime)
return s
class ABCD(AC):
a1 = AC( 0xADFC , True , 2)
a2 = AC( 13 , False ,5)
print a1
print a2
I want to encode it and then store it in a variable…..So how can i do it???
For C structures, all you have to do to write it to a file is open the file, then do
Then you can reload it by opening the file and doing
All you need is to make your
__init__arguments optional. See this post on Binary IO. It explains how to sendStructureover sockets or withmultiprocessingListeners.To save it in a string / bytes just do
Then read it back out with
Pickle and the like are not necessary. Neither is
struct.packthough it would work. It’s just more complicated.Edit: also see the linked answer at How to pack and unpack using ctypes (Structure <-> str) for another method for doing this.
Edit 2: See http://doughellmann.com/PyMOTW/struct or http://effbot.org/librarybook/struct.htm for struct examples.