Reputation: 161
I have a C string, it is encoded with UTF-7, like "+g0l6P3ux-"
.
I can decode the string with Python. But when I use:
[NSString stringWithCString:"+g0l6P3ux-" encoding:kCFStringEncodingUTF7];
it returns nil
, why ?
Upvotes: 4
Views: 589
Reputation: 11
I managed to get correct text with this lines of code - initial text was in UTF 7 (mail folder path in russian):
const char *stringAsChar = [folder.folderPath cStringUsingEncoding:[NSString defaultCStringEncoding]];
NSString *str = [NSString stringWithCString:stringAsChar encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF7_IMAP)];
Upvotes: 1
Reputation: 1496
The function you use
+ (id)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc
expects NSStringEncoding, and if you look up NSStringEncoding in the docs, kCFStringEncodingUTF7 is not a valid value.
Instead of
NSString *str = [NSString stringWithCString:"+g0l6P3ux-" encoding:kCFStringEncodingUTF7];
you could use
CFStringRef cfstr = CFStringCreateWithCString(NULL, "+g0l6P3ux-", kCFStringEncodingUTF7);
NSString *str = [(NSString *)cfstr copy];
CFRelease(cfstr);
Upvotes: 4