NSString to const char * convert with Greek characters

前端 未结 1 1736
野的像风
野的像风 2021-01-17 03:16

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\

相关标签:
1条回答
  • 2021-01-17 03:41

    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];
    
    0 讨论(0)
提交回复
热议问题