[IronPython] How to convert a 'str' type to byteArray

Toni Alatalo antont at kyperjokki.fi
Thu Nov 12 05:21:35 CET 2009


Vernon Cole kirjoitti:
> Make sure what data type is really being returned by part.getpayload() 
> .  Perhaps it is already in a suitable for when you get it?

Before he posted to the list we actually talked about this on irc too, 
and it seemed that it was unicode. He was seeing type 'str', but it 
seemed that str is unicode on ironpy, so probably that's why giving it 
to create a byte array doesn't work.
> s = 'this is a string'
> b = buffer(s)
> stores "strings" in unicode, and buffer() tries to be smart about 
> hiding that fact, in order to act like CPython in the easy cases.  The 
> above will work the same in either IronPython or CPython.

Ok, AFAIK he wasn't trying buffer() yet and I also didnt't know much 
about it, so perhaps this is it? I'm sure he'll try soon.

> (IronPython seems to store them internally in UTF-8 or some similar 
> code.)  To convert to "proper" 8-bit codes you must use something like:
> u = u'unicode string'
> b = buffer(u.encode('latin-1'))

The string/bytes is an image from an email (using some .net lib i guess, 
dunno on what that getpayload() is), I wonder if something like 
'latin-1' is still the thing to use :o

> Python 3.x removes the problem by defining all character strings as 
> unicode (as IronPython does now), and providing a byte() function, in 
> which each 8 byte is treated as an integer, rather than a character. 
> That will make things simpler in the future.

Indeed, perhaps the main reason why am looking forward to living in that 
land eventually.

Thanks for info! (I and the OP work in the same overall project 
(http://www.realxtend.org/ open source virtual worlds / network games 
plat), even though I'm not working on the same thing he is now (we use 
ironpy on the server and cpython on the client, I'm mostly busy with the 
client side right now)

> VC

~Toni

> On Wed, Nov 11, 2009 at 8:14 AM, matan keret <matan504 at gmail.com 
> <mailto:matan504 at gmail.com>> wrote:
>
>     hi,
>      
>     I have the following code which gets an image from an email.
>     after getting it i need to pass it as a byteArray (byte[] in C#)
>     to a C# function.
>     i tried all sorts of things but no luck. I'm using ironPython 2.0.3
>      
>     the important parts of the code are:
>      
>     # getting the image from the e-mail
>     image = part.get_payload()
>      
>     # my try to cast the str into a byteArray
>     byteArray = BitConverter.GetBytes(image.ToCharArray())
>      
>      
>     this last line returns 1 byte and doesn't seem to do the job.
>     is there any equivalent to the 'bytearray(image)' function in CPython?
>      
>     thanks,
>     Matan
>      
>
>     _______________________________________________
>     Users mailing list
>     Users at lists.ironpython.com <mailto:Users at lists.ironpython.com>
>     http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>   




More information about the Ironpython-users mailing list