• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Making an NSString from NSData
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Making an NSString from NSData


  • Subject: Re: Making an NSString from NSData
  • From: Brian Webster <email@hidden>
  • Date: Mon, 22 Oct 2001 23:57:20 -0500

On Monday, October 22, 2001, at 09:29 PM, cocoa-dev-
email@hidden wrote:

How do I make an NSString into NSData? Do have to go through every byte and
put that into a string? I don't really know what NSData is because it can
contain images (as I learned from cocoadevcentral's image buddy) and I just
don9t know what its content would be. Sorry for the newbie question, but I
couldn't do it when I tried.

An NSData object just maintains a buffer of raw bytes in memory. The bytes can be anything - text, image data, or whatever else you want. The easiest way to transform an NSString into an NSData is to send the string a -dataUsingEncoding: message, like so:

NSString *myString;
NSData *myData;

myData = [myString dataUsingEncoding:NSMacOSRomanStringEncoding];

The encoding is important, as it will determine how each character is written into the data object. The above I believe is the default string encoding for OS X. If you want unicode text (each character takes 2 bytes), use NSUnicodeStringEncoding. All the possible choices are declared at the top of NSString.h.

--
Brian Webster
email@hidden
http://homepage.mac.com/bwebster


  • Prev by Date: Re: Drop location on the Finder?
  • Next by Date: Re: Paradigm Shifting: Observations & Questions
  • Previous by thread: Re: Making an NSString from NSData
  • Next by thread: converting C-strings to NSString
  • Index(es):
    • Date
    • Thread