Pay attention to 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);
When launched from a completely new Foundation project, this program outputs:
C string comparison: 0
str1: "√§"
str2: "√§"
This is really what I expect, because the lines should be the same.
However, if I run this exact code somewhere deep in a different code base, I get this output:
C string comparison: 31
str1: "√§"
str2: "√§"
What can explain this difference? I am sure that both files are in UTF-8 encoding. These are different file encodings - the only possible explanation for this behavior, right?
Any ideas what could go wrong in the second case? How can i fix this?
(Perhaps it is worth mentioning that in the second case, the code runs in a file .mm, i.e. under Objective-C ++. Could this be an explanation for this?)