some more doubts:? RE: [Tutor] How to convert integer to binay packed string...,?
Ajaya Babu
ajaya@ncoretech.com
Wed, 12 Sep 2001 16:36:56 +0530
Hi,
I've some more doubts regarding serilizing objects and converting integer
data to binary packed string.
First when I use struct module...there is problem I am facing.., My code
looks like
class EventDial(Event):
def __init__(self, Pipe, DigitStr):
Event.__init__(self, Pipe)
self.DialedNumber = DigitStr
print self.DialedNumber
self.EventType = 1
def Serialize(self):
a = len(self.DialedNumber)
data = pack('IIs', self.EventType, len(self.DialedNumber),
self.DialedNumber)
self.Pipe.SendData(data)
but packing data using 'IIs' givign problem when my string size is more than
1. For example if I dail a number 911 my string size is 3 when I pack and
receive at the toher end
It is giving like this...,
1l, 3l, '9'
When i went through the documentation i got a feeling that I should give
sring size as a prefix to the 's' parameter. But i've variable string
lenght. So How can I use this prefix notation...,?
Second thing when I tried using pickle. I confused with the documentation.
there is a pcikle.dump(obj, file, bin=0) function.., but it always needs
file. If I want to send to network (LAN) then why I need to have a file
operation in between it is slow right? If I don't need to do this plese give
me example or link to the docs. I quite confused with the pickle...,
Thanks and Regards,
Ajaya Babu
-----Original Message-----
From: tutor-admin@python.org [mailto:tutor-admin@python.org]On Behalf Of
Danny Yoo
Sent: Wednesday, September 12, 2001 12:34 AM
To: Ajaya Babu
Cc: PythonTutorlist (E-mail)
Subject: Re: [Tutor] How to convert integer to binay packed string...,?
On Tue, 11 Sep 2001, Ajaya Babu wrote:
> I've a small doubt, How to convert integers into bytestream in python.
> What actually I want to do is send integer to another system in the
> network. But since there is nothing like integer in python...I am just
> wondering how can
Actually, Python Integers are 32-bit integers, and we can do bitwise
manipulation with them:
###
>>> x = 2
>>> x>>2
0
>>> x<<2
8
>>> 0xffffffff & 0x00002000
8192
###
As Ignacio as mentioned, it sounds like you'll want to look at the
"struct" module, a module that deals exclusively with packing things into
bytes.
Alternatively, you might want to look at the "pickle" module:
http://python.org/doc/current/lib/module-pickle.html
pickle will allow us to preserve integers into strings --- in effect, it
turns things into a bytestream. pickle will "serialize" Python objects
into something that we can save, or send! If you're sending something
across the network to another Python system, pickle might be useful for
you.
Hope this helps!
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor