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

Michael Foord fuzzyman at voidspace.org.uk
Wed Nov 11 07:58:44 PST 2009


matan keret 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?

I have no idea about the *specific* APIs you mention, but you can turn a 
string into a byte array with code like this (ignoring encoding issues):

    from System import Array, Byte

    byteArray = Array[Byte](ord(c) for c in some_string)

HTH!

Michael


>  
> thanks,
> Matan
>  
> ------------------------------------------------------------------------
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>   


-- 
http://www.ironpythoninaction.com/



More information about the Users mailing list