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

matan keret matan504 at gmail.com
Thu Nov 12 10:41:54 CET 2009

Thank you all for your help!

i tried the buffer idea, but it gave me a 'buffer' type and not a byteArray.
so i tried Michael's idea, which gave me some error. but then I found this
post: http://www.smallshire.org.uk/sufficientlysmall/tag/ironpython/

I fixed the code to:
from System import Array, Byte
a = Array[Byte](tuple(Byte(ord(c)) for c in image))

which seems to work :-)

Like Tony said, Thanks a lot for the info!

On Thu, Nov 12, 2009 at 6:21 AM, Toni Alatalo <antont at kyperjokki.fi> wrote:

> 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
>> _______________________________________________
>> Users mailing list
>> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20091112/48578f1e/attachment.html>

More information about the Ironpython-users mailing list