little boy
little boy

Reputation: 1221

convert NSString to const char *

I want to convert the nsstring value into const char *.

NSString *s = @"יככעימבבגיננימ"; //Hebrew characters
const char *t = [s cStringUsingEncoding:NSUTF8StringEncoding];
NSLog(@"\n str = %s",t);

The console showed like this "יככעימבבגיננימ".

How to get the actual NSString value into const char*?

Thanks, loganathan

Upvotes: 2

Views: 3852

Answers (1)

Krizz
Krizz

Reputation: 11542

It's just because %s in NSLog denotes ASCII string (plain-C 8 bit strings, to be precise), not UTF8.

What about trying the following?:

NSString *s = @"יככעימבבגיננימ"; //Hebrew characters
NSLog(@"\n str = %@",s);

You can log out UTF16 array, also:

const wchar_t *t = (const wchar_t*)[s cStringUsingEncoding:NSUTF16StringEncoding];
NSLog(@"\n str2 = %S",t);

Upvotes: 6

Related Questions