3

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???

10
  • 4
    What do you mean by "encode it"? Commented Aug 11, 2011 at 6:42
  • I'm not sure I understand exactly what you want. But if it's what I'm guessing you could use from hashlib import md5 and md5(String_here).hexdigest() Commented Aug 11, 2011 at 6:46
  • Like if i have to send this msg to a receiver i will send it in an encoded form.... Commented Aug 11, 2011 at 6:46
  • What is the "it" you're trying to encode? Commented Aug 11, 2011 at 6:47
  • So you mean you want to encrypt it, or what? Encode can mean anything, from "encode in ASCII" or "encode in unicode" or even "encode in compressed format". Encrypting, on the other hand, also requires sending the "key" that wil decrypt your message on the other end. Commented Aug 11, 2011 at 6:48

1 Answer 1

4

For C structures, all you have to do to write it to a file is open the file, then do

fileobj.write(my_c_structure).

Then you can reload it by opening the file and doing

my_c_structure = MyCStructure()
fileobj.readinto(my_c_structure)

All you need is to make your __init__ arguments optional. See this post on Binary IO. It explains how to send Structure over sockets or with multiprocessing Listeners.

To save it in a string / bytes just do

from io import BytesIO # Or StringIO on old Pythons, they are the same
fakefile = BytesIO()
fakefile.write(my_c_structure)
my_encoded_c_struct = fakefile.getvalue()

Then read it back out with

from io import BytesIO # Or StringIO on old Pythons, they are the same
fakefile = BytesIO(my_encoded_c_struct)
my_c_structure = MyCStructure()
fakefile.readinto(my_c_structure)

Pickle and the like are not necessary. Neither is struct.pack though 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.