[IronPython] Convert a PythonBuffer to a byte[]

Dino Viehland dinov at microsoft.com
Tue Apr 27 03:49:35 CEST 2010

Jeff wrote:
> I can't seem to find any easy to convert a PythonBuffer (created in
> Python and passed to C#) to a byte[]. The best solution I've found so
> far is to call __getslice__ to get a string and convert that to a
> byte[]. Is there a better way to so this? If not, will __getslice__
> always return a string, or will it depend on how the buffer was
> created?

I think __getslice__ can return non-strings - it might return ByteArray
objects or array objects from the array module.  I would suggest calling 
ToString() and converting that to bytes.

We should probably expose some way to get at the bytes directly though.
We might even want to expose an IntPtr which could have the address of 
the bytes if they're available - given that's the basic reason for buffer
objects existence.

More information about the Ironpython-users mailing list