Re: Manipulating raw data in Python

Discussion in 'Python' started by Chris Mellon, Aug 21, 2007.

  1. Chris Mellon

    Chris Mellon Guest

    On 8/21/07, Looney, James B <> 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.
    Chris Mellon, Aug 21, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian
    Replies:
    2
    Views:
    1,909
    Brian
    Dec 24, 2003
  2. Simnet
    Replies:
    0
    Views:
    366
    Simnet
    Dec 3, 2004
  3. =?Utf-8?B?Smlt?=

    Manipulating Excel data with ASP.NET

    =?Utf-8?B?Smlt?=, Nov 2, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    6,797
    =?Utf-8?B?Smlt?=
    Nov 3, 2005
  4. Replies:
    2
    Views:
    460
  5. Peng Yu
    Replies:
    5
    Views:
    1,043
    Peng Yu
    Oct 19, 2009
Loading...

Share This Page