2009-12-07 47 views
9

我在我的應用程序(英語,西班牙語,意大利語)中有本地化。客戶端向我發送了字符串文件,但有些字符很奇怪,我如何糾正它們?我發現客戶端使用的是Mac OS Roman編碼,我如何將其轉換爲utf-8。 例如紐倫堡 被轉換爲N√ºrnberg,當客戶端現在發送給我,我想重新轉換它。如何將iphone sdk中的NSString轉換爲UTF-8格式的字符串?

回答

12

如果你有一個屬性與所有字符串的文件...你可以在加載它們和任何與這些閱讀它們(從NSString's Documentation):

– initWithBytes:length:encoding: 
– initWithBytesNoCopy:length:encoding:freeWhenDone: 
– initWithCString:encoding: 
– initWithUTF8String: 

這裏的所有可能的編碼

名單
enum { 
    NSASCIIStringEncoding = 1, 
    NSNEXTSTEPStringEncoding = 2, 
    NSJapaneseEUCStringEncoding = 3, 
    NSUTF8StringEncoding = 4,     <----- UTF8! 
    NSISOLatin1StringEncoding = 5, 
    NSSymbolStringEncoding = 6, 
    NSNonLossyASCIIStringEncoding = 7, 
    NSShiftJISStringEncoding = 8, 
    NSISOLatin2StringEncoding = 9, 
    NSUnicodeStringEncoding = 10, 
    NSWindowsCP1251StringEncoding = 11, 
    NSWindowsCP1252StringEncoding = 12, 
    NSWindowsCP1253StringEncoding = 13, 
    NSWindowsCP1254StringEncoding = 14, 
    NSWindowsCP1250StringEncoding = 15, 
    NSISO2022JPStringEncoding = 21, 
    NSMacOSRomanStringEncoding = 30, 
    NSUTF16StringEncoding = NSUnicodeStringEncoding, 
    NSUTF16BigEndianStringEncoding = 0x90000100, 
    NSUTF16LittleEndianStringEncoding = 0x94000100, 
    NSUTF32StringEncoding = 0x8c000100, 
    NSUTF32BigEndianStringEncoding = 0x98000100, 
    NSUTF32LittleEndianStringEncoding = 0x9c000100, 
    NSProprietaryStringEncoding = 65536 
}; 
19

簡單方法

[yourNSStringObject UTF8String]; 
相關問題