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 7962809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:26:59+00:00 2026-06-04T05:26:59+00:00

Please consider the following code: NSString *string = @ä; const char *str1 = [string

  • 0

Please consider the following code:

NSString *string = @"ä";
const char *str1 = [string cStringUsingEncoding:NSUTF8StringEncoding];
const char *str2 = "ä";
NSLog(@"C string comparison: %d",strcmp(str1,str2));
NSLog(@"str1: \"%s\"", str1);
NSLog(@"str2: \"%s\"", str2);

If run from a brand-new Foundation project, this program outputs:

C string comparison: 0
str1: "√§"
str2: "√§"

This is indeed what I expect to happen, because the strings are supposed to be the same.

However, if I run this exact same code somewhere deep within another codebase, I get this output:

C string comparison: 31
str1: "√§"
str2: "√§"

What could possibly explain this difference? I’m pretty sure both files are in the UTF-8 encoding. That — different file encodings — is the only possible explanation for this behavior, right?

Any ideas what could have gone wrong in the second case? How can I fix it?

(I should maybe mention that in the second case, the code is being run in an .mm file, i.e. under Objective-C++. Can that be an explanation for this?)

  • 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-04T05:27:02+00:00Added an answer on June 4, 2026 at 5:27 am

    How a source file is encoded on disk is one thing. How the compiler believes it is encoded is another. By default, GCC assumes UTF-8 but it can be told it’s in another encoding from the locale or the -finput-charset=<charset> option. I expect that Clang supports the same thing.

    Xcode has its own notion of the encoding of a source file. I don’t know if it adjusts the compile command to pass that in using the above option, but I wouldn’t be surprised.

    GCC also has a notion of the execution character set. This is how it writes strings into the binary. See the -fexec-charset=<charset> option.

    So, the compiler interprets the bytes of the file according to the input character set and writes them out to the binary in the execution character set. If those two differ, then that involves a conversion. This is a per-translation unit affair, so it can happen differently for different source files.

    Another issue is that “ä” has two possible representations in Unicode. It can be LATIN SMALL LETTER A WITH DIAERESIS (U+00E4) or it can be LATIN SMALL LETTER A (U+0061) followed by COMBINING DIAERESIS (U+0308). In UTF-8, that would be 0xC3 0xA4 vs. 0x61 0xCC 0x88. Your two source files may express the same character differently, which means they really do contain different strings (at all levels: C string, NSString, whatever, although NSString will ignore that difference for -compare:... methods if NSLiteralSearch is not specified; the -isEqual... methods do a literal compare, though). This would, of course, be exacerbated if those two byte sequences were being converted among encodings in different ways.

    So, you need to track down the specific source files which contain the relevant strings. Check with a hex dump exactly which bytes they contain. Check the commands used to compile them (and possibly the environments if locale may play a role) to see what the compiler believes about the input and executable character sets.

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

Sidebar

Related Questions

Please consider the following code: public class Person ( public string FirstName {get; set;}
Please consider the following code : string[] words = { Too, much, of, anything,
Please clarify my doubt in Hashset. Consider the following code, class Person { String
Please consider following code: 1. uint16 a = 0x0001; if(a < 0x0002) { //
Please consider the following code, struct foo { foo() { std::cout << Constructing! <<
Please consider the following example code (from the lm doc): ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt
Please consider the following code: #include <iostream> #include <typeinfo> template< typename Type > void
Please consider the following code, <form action=index.php method=post name=adminForm enctype=multipart/form-data> <input type=hidden name=showtime[] id=showtime_1
Please consider the following code: class Abase{}; class A1:public Abase{}; class A2:public A1{}; //etc
please consider the following code: template <typename T> struct foo { template <typename S>

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.