Convert NSString to NSData

It’s a common question among iOS developers, how do I convert an NSString object to an NSData object?

The conversions is as simple as shown below:

If for some reason you are dealing with content that has an unknown encoding, you may have better luck with something like this:

The defaultCStringEncoding returns a value that is based on the user’s default language among other factors.