Manipulating raw data in Python

Chris Mellon arkanes at gmail.com
Tue Aug 21 19:48:04 CEST 2007


On 8/21/07, Looney, James B <james.b.looney at lmco.com> wrote:
>
>
>
> How do I get access to a data buffer in Python so that I can directly
> view/modify the data?  My buffer size is 256 (for this specific case) bytes.
>  Most of the time, I access want to access that data in 4-byte chunks,
> sometimes in single byte chunks.  How do I go about doing so?
>
> I'm certain someone's come across (and has probably asked this question a
> few times).  I've tried various searches, and I'm either missing the
> obvious, or am not using the right key words to get what I'm looking for.  I
> mostly get back results about audio, files, etc.  In my case, I'm using an
> HDLC protocol.
>
> A little background:
> I've got a socket connection.  On the server side is C++ code.  On the
> client (Python) side, I have a SWIG-ified C++ extension.  The extension is
> my messaging API, so that I can interact with the socket in a known manner
> on both sides of the connection.  In C++, I'd usually just use a method like
> "char *getBuffer()". However, Python seems to want to treat this as a string
> even though it really isn't.  I usually only get a few bytes before Python
> encounters data which looks like a NULL.
>

Python strings are mis-named and are are actually byte sequences. They
can contain arbitrary data, including NULLs. Your SWIG wrapper is
probably buggy and is using PyString_FromString instead of
PyString_FromStringAndSize.



More information about the Python-list mailing list