Re: Newbie: Encoding NSData to a string
Re: Newbie: Encoding NSData to a string
- Subject: Re: Newbie: Encoding NSData to a string
- From: James Bucanek <email@hidden>
- Date: Sun, 25 Mar 2007 22:39:38 -0700
Joao Lourenco wrote on Monday, March 26, 2007:
>I'm starting to learn Cocoa and would like to do the following
>
>Read an image from a file
>Encode the image as an "ascii" string
>Send the image as an ASCII string in the body of a post message
>
>This is the code I have so far:
<clip>
>NSString* stringEncodedPicture = [[NSString alloc] initWithData:
>dataEncodedPicture encoding: NSUTF8StringEncoding];
This will not work. +[NSString initWithData:...] interpret a sequences of bytes as an encoded string. An image is not an encoded string.
What you need to do is _encode_ the sequence of bytes into a string. I'd suggest base64. This is what XML and property lists use to encode arbitrary binary data. There should be a base64 encoding routine handy, I just can't remember where to fine one at the moment. Maybe someone else knows.
--
James Bucanek
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden