简体   繁体   中英

NSString to const char * convert with Greek characters

I'm trying to convert an NSString which contains a greek word to a const char. I'm trying to convert the string with UTF-8 encoding which is for greek language and when i'm logging the char out, it has junk in it. Please a little help here..

//this is the greek word 
NSString *letter = textFieldLetter.text;

//NSString to const char conversion for the sql query
const char *cLetter = (const char *)[letter cStringUsingEncoding:NSUTF8StringEncoding]; 

//this contains junk!
NSLog(@"letter: %s",cLetter);

You don't have to use UTF8, instead use Unicode ( NSUnicodeStringEncoding ).

If you see the NSStringEncoding will see this options (have more):

enum {
NSWindowsCP1253StringEncoding = 13,    /* Greek */
NSUTF16StringEncoding = NSUnicodeStringEncoding,   

NSUTF16BigEndianStringEncoding = 0x90000100,          /* NSUTF16StringEncoding encoding with explicit endianness specified */
NSUTF16LittleEndianStringEncoding = 0x94000100,       /* NSUTF16StringEncoding encoding with explicit endianness specified */

NSUTF32StringEncoding = 0x8c000100,                   
NSUTF32BigEndianStringEncoding = 0x98000100,          /* NSUTF32StringEncoding encoding with explicit endianness specified */
NSUTF32LittleEndianStringEncoding = 0x9c000100        /* NSUTF32StringEncoding encoding with explicit endianness specified */
};
typedef NSUInteger NSStringEncoding;

You can see a specific Greek lenguage: NSWindowsCP1253StringEncoding

If is don't work, you can try to use other UTF16 or UTF32. But I think Unicode work for you.

-- edit

Take in mind that NSLog(@"%s",string); can not decode a unicode character. So, it will really show the mess.

To test if it work, do the conversion, and redo it. If is the same, so you don't lost data.

const char *cLetter = (const char *)[letter cStringUsingEncoding:NSUnicodeStringEncoding]; 
NSString *original = [NSString stringWithCString:cLetter encoding:NSUnicodeStringEncoding];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM