UTF8 character decoding in Objective C

后端 未结 4 2051
[愿得一人]
[愿得一人] 2020-12-01 02:17

I am trying to parse a feed from a json webservice on my iPhone however the utf8 conversion is not working the way it should or am I doing something wrong Here is apart of

4条回答
  •  醉话见心
    2020-12-01 03:05

    I think the test case is broken; the following:

    NSString* str1 = @"est un r \u00c3\u00aa ve en noir";
    NSLog(@"%@", str1);
    

    Also outputs 'est un r ê ve en noir'. However, this:

    NSString* str1 = @"est un rêve en noir";
    NSLog(@"%@", str1);
    

    Outputs 'est un rêve en noir', as does:

    NSString* str1 = @"est un rêve en noir";
    
    NSString* str = [NSString stringWithUTF8String:[str1 cStringUsingEncoding:NSUTF8StringEncoding]];
    NSLog(@"%@", str);
    

    And ditto for the slightly shorter version:

    NSString* str1 = @"est un rêve en noir";
    
    NSString* str = [NSString stringWithUTF8String:[str1 UTF8Encoding]];
    NSLog(@"%@", str);
    

    And, indeed:

    char *str1 = "est un r\xc3\xaave en noir";
    
    NSString* str = [NSString stringWithUTF8String:str1];
    NSLog(@"%@", str);
    

    I think it's a question of JSON, not UTF8. The \u followed by four hexadecimal digits is JSON's way of encoding a generic UTF16 character, it's not an inherent part of UTF. So NSString doesn't know how to deal with it. Your JSON parser needs to be adapted to parse escape sequences properly.

提交回复
热议问题