|
View:
New views
5 Messages
—
Rating Filter:
Alert me
|
|
|
How to convert a 'str' type to byteArrayhi,
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@... http://lists.ironpython.com/listinfo.cgi/users-ironpython.com |
|
|
Re: How to convert a 'str' type to byteArraymatan 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@... > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com > -- http://www.ironpythoninaction.com/ _______________________________________________ Users mailing list Users@... http://lists.ironpython.com/listinfo.cgi/users-ironpython.com |
|
|
Re: How to convert a 'str' type to byteArrayMake sure what data type is really being returned by part.getpayload() . Perhaps it is already in a suitable for when you get it?
The Python 2.x language method of loading a binary blob of memory is to use the built in function "buffer" s = 'this is a string' b = buffer(s) "b" will be accepted my a byte arrary in most cases. but make sure that what you have is a legal ASCII string. CPython stores "strings" in ASCII and "unicode" differently. Iron Python 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. On the other hand, in CPython: u = u'this is a unicode string' b = buffer(u) will produce a blob twice (or four times) as long as the above, since it stores all 16 (or 32) bits of the character code. (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')) 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. -- VC On Wed, Nov 11, 2009 at 8:14 AM, matan keret <matan504@...> wrote:
_______________________________________________ Users mailing list Users@... http://lists.ironpython.com/listinfo.cgi/users-ironpython.com |
|
|
Re: How to convert a 'str' type to byteArrayVernon 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@... > <mailto:matan504@...>> 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@... <mailto:Users@...> > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com > > > ------------------------------------------------------------------------ > > _______________________________________________ > Users mailing list > Users@... > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com > _______________________________________________ Users mailing list Users@... http://lists.ironpython.com/listinfo.cgi/users-ironpython.com |
|
|
Re: How to convert a 'str' type to byteArrayThank 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)) Like Tony said, Thanks a lot for the info!
On Thu, Nov 12, 2009 at 6:21 AM, Toni Alatalo <antont@...> wrote: Vernon Cole kirjoitti: _______________________________________________ Users mailing list Users@... http://lists.ironpython.com/listinfo.cgi/users-ironpython.com |
| Free embeddable forum powered by Nabble | Forum Help |