Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8698489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:44:32+00:00 2026-06-13T01:44:32+00:00

I understand why when going from NSData to NSString you need to specify encoding.

  • 0

I understand why when going from NSData to NSString you need to specify encoding.
However I’m finding it frustrating how the reverse (NSString to NSData) needs to have an encoding specified.

In this related question the answers suggested using
NSUTF8StringEncoding or defaultCStringEncoding, with the latter not being fully explained.

So I just wanted to ask IF the following is correct when converting NSString to NSData:

  • In cases where you want to be 100% sure the binary representation of the NSString object is UTF8 then use NSUTF8StringEncoding (or whatever encoding is needed)

  • In cases where the encoding of the NSString object is known/expected to already be of a certain type and no conversion is required then it’s safe (perhaps internally faster) to use defaultCStringEncoding (from what I have read objective-c uses UTF-16 internally, not sure if LE or BE but I’d assume LE because the platform is LE)

TIA

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:44:33+00:00Added an answer on June 13, 2026 at 1:44 am

    The encoding needs to be specified for converting NSString to NSData for the same reason it needs to be specified going from NSData to NSString.

    An NSData object is a wrapper for a string of absolutely raw bytes. If the NSString doesn’t specify some encoding, it doesn’t know what to write, because at the level of ones and zeroes, a UTF-16 encoding looks different from a UTF-8 encoding of the same letter, and of course, if you write UTF-16 as big-endian and read it as little-endian you will get gibberish.

    In other words, don’t think of it as converting or escaping a string; it’s generating a byte buffer, and the encoding tells it which ones and zeroes to write when the next character is “a” and which ones to write when it means “妈”.

    As for your question…here’s my two cents.

    1) If you are converting an NSString to an NSData so that your same program can convert it back later, and no other software will need to deal with that NSData until after you’ve read it back into an NSString, then none of this matters. All that matters is that your string-to-data encoding and your data-to-string encoding match.

    2) If you are dealing only with ASCII characters, you can probably get away with a lot, just because many kinds of encoding use the same representation for characters under 128. But this breaks easily, even with little things like smart quotes.

    3) Despite the name, defaultCStringEncoding is not something you should use as a default. It’s designed for special circumstances where you need to deal with system strings and don’t otherwise know how the system deals with its internal strings. It refers to the way strings are handled in the default C implementation, NOT in the NSString internals, so there’s not necessarily a performance benefit.

    4) If you write a string with an unknown string encoding, and you try to read it back with a different string encoding, your code will fail; in many cases, you will just end up with an empty string.

    Bottom line is: who will be trying to interpret your NSData objects? If it’s your own app, pick an encoding that makes sense for you (I use UTF8 for everything) and use it for both conversions. Otherwise, figure out what your ecosystem needs to read or write and make that your standard.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have issues with NSUserDefaults and I don't quite understand what is going on.
I have been learning ASP.NET MVC recently and everything is going well apart from
I really don't understand what's going on here. I have a function that is
I dont really understand whats going on and cant see the difference: I'm downloaded
Can anyone help me understand what is going on here? Jenkins has been working
I was never able to fully understand what is going on in the background
I'm going through serialization and I can't understand the following: I don't understand why
I'm going a little nuts trying to understand the doc on impersonation and delegation
I don't understand why the result is going to be 36. Can somebody explain
Please help me understand where I am going wrong so much so that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.