将unicode字符串转换为utf8

时间:2011-04-26 14:50:43

标签: iphone unicode utf-8 nsstring

当我得到一个形式为\ u043F \ u043F(Unicode)的字符串时,如何将其转换为可读的NSUT8String?这是我的代码(当这些是非英文字符时失败):

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

    NSString *theStr = [[NSString alloc]  initWithBytes:[receivedData bytes]
                                                  length:[receivedData length] encoding: NSUTF8StringEncoding];

    NSLog(theStr);
}

当字符串是英文字符时,一切都很好 - 但是当它是Unicode格式时,它无法给我一个可读的字符串(但仍然是Unicode格式)。

您怎么看?


编辑:

我意识到我没有提供足够的信息来解释我正在尝试做什么。我试图使用youtube的方式来获取自动建议的关键字当你使用搜索框(没有官方,只是使用嗅探器找出)。这是:

http://suggestqueries.google.com/complete/search?hl=en&client=youtube&hjson=t&ds=yt&jsonp=window.yt.www.suggest.handleResponse&q=*******&cp=******

q是您的查询,cp是q的长度。

所以基本上当q是英语的东西时它工作正常。但是当q有非英文字符(例如俄语)时,这就是我得到的(来自NSLog):

window.yt.www.suggest.handleResponse(["\u043F\u0440",[["\u043F\u0440\u0438\u043A\u043E\u043B\u044B","","0"],["\u043F\u0440\u043E\u0436\u0435\u043A\u0442\u043E\u0440\u043F\u0435\u0440\u0438\u0441\u0445\u0438\u043B\u0442\u043E\u043D","","1"],["\u043F\u0440\u043E\u0436\u0435\u043A\u0442\u043E\u0440\u043F\u0435\u0440\u0438\u0441\u0445\u0438\u043B\u0442\u043E\u043D 87","","2"],["\u043F\u0440\u043E\u0436\u0435\u043A\u0442\u043E\u0440\u043F\u0435\u0440\u0438\u0441\u0445\u0438\u043B\u0442\u043E\u043D 88","","3"],["\u043F\u0440\u043E\u0436\u0435\u043A\u0442\u043E\u0440\u043F\u0435\u0440\u0438\u0441\u0445\u0438\u043B\u0442\u043E\u043D 86","","4"],["\u043F\u0440\u043E\u0436\u0435\u043A\u0442\u043E\u0440\u043F\u0435\u0440\u0438\u0441\u0445\u0438\u043B\u0442\u043E\u043D 85","","5"],["\u043F\u0440\u043E\u0436\u0435\u043A\u0442\u043E\u0440\u043F\u0435\u0440\u0438\u0441\u0445\u0438\u043B\u0442\u043E\u043D 89","","6"],["\u043F\u0440\u043E\u0436\u0435\u043A\u0442\u043E\u0440\u043F\u0435\u0440\u0438\u0441\u0445\u0438\u043B\u0442\u043E\u043D 84","","7"],["\u043F\u0440\u0438\u043A\u043E\u043B\u044B \u0432 \u043F\u0440\u044F\u043C\u043E\u043C \u044D\u0444\u0438\u0440\u0435","","8"],["\u043F\u0440\u043E\u0436\u0435\u043A\u0442\u043E\u0440\u043F\u0435\u0440\u0438\u0441\u0445\u0438\u043B\u0442\u043E\u043D 90","","9"]],{}])

2 个答案:

答案 0 :(得分:1)

您可以使用:

@interface NSString 
{
     - (__strong const char *)UTF8String;  // Convenience to return
                                           // null-terminated UTF8 representation
}

答案 1 :(得分:0)

我认为这可能会有所帮助..

NSString *yourString = "\u043F\u0440\u0438\u043A\u043E\u043B\u044B";
NSArray *unicodeArray = [yourString componentsSeparatedByString:@"\\u"];
NSMutableString *finalString = [[NSMutableString alloc] initWithString:@""];
for (NSString *unicodeString in unicodeArray) {
    if (![unicodeString isEqualToString:@""]) {
        unichar codeValue;
        [[NSScanner scannerWithString:unicodeString] scanHexInt:&codeValue];
        NSString* betaString = [NSString stringWithCharacters:&codeValue length:1]; 
        [finalString appendString:betaString];
    }
} 
//finalString should have encoded one