I have a UTF-8 encoding string that I want to display in a label.
When I set a break-point and examine the variable holding the string, all looks good. However, when I try to output to the log, or to the label, I get latin encoding.
I have tried almost every suggestion on SO and beyond, but I just cannot get the string to display properly.
Here is my code:
NSString *rawString = [NSString stringWithFormat:@"%@",m_value];
const char *utf8String = [rawString UTF8String];
NSLog (@"%@", [NSString stringWithUTF8String:utf8String]);
NSLog (@"%s", utf8String);
NSLog (@"%@", rawString);
self.resultText.text = [NSString stringWithUTF8String:utf8String];
m_value is an NSString, and in the debug window, it also displays the correct encoding.
m_value NSString * 0x006797b0 @"鄧樂愚..."
NSObject NSObject
isa Class 0x3bddd8f4
[0] Class
I am using the iOS 6.1 SDK.
Considering your variable m_value NSData, you can try the following
There are many encoding available you can try them too
So I finally managed to get to the bottom of this.
The
m_value
NSString
was being set by a third party library to which I had no access to the source. Even though the value of this variable was being decoded correctly in the (I.e. displaying the Chinese characters) in the debug panel, the string was actually encoded withNSMacOSRomanStringEncoding
.I was able to determine this by copying the output into TextWrangler, and flipping encodings until I found the one that translated correctly into UTF-8.
Then to fix in Objective-C, I first translated the
NSString
to aconst char
:Then converted back to an
NSString
:+1 to @Vitaly_S and @iphonic whose answers eventually led me to this solution. For anyone else that stumbles across this; it seems that as of Xcode 4.6.1, the debug window cannot be trusted to render strings correctly, but you can rely on the
NSLog
output.Ok, if
m_value
is a const char contained UTF-8 string you have to use this method:- (id)initWithUTF8String:(const char *)bytes
It's incorrect to pass
const char*
to@
formatter, because@
meansNSObject
, so it will be always incorrect and can lead to app crashWhen I want to show khmer on label, I use font 'Hanuman.ttf'. This is code I use:
I don't know this can help you or not , but this is what I did before !