我正在检索包含UTF8或ASCII编码文本的HTML。对于最常见的用途,它是ASCII解码,用于显示正确的文本:
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
现在我有另一个带有UTF8编码的HTML页面,所以我必须使用:
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
加载网站时,我检索的哪种编码是随机的。我的问题是,有没有办法检查NSData是什么类型的解码是使用权?所以我知道我需要使用哪种编码类型。
日Thnx!
答案 0 :(得分:4)
我不知道是否可以检查NSData的编码,所以这就是我所做的:
NSString *dataStr;
dataStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
if (!dataStr)
{
NSLog(@"ASCII not working, will try utf-8!");
dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
//Do stuff with dataStr
答案 1 :(得分:3)
尽管Heliem的回答很有用,但如果ASCII和UTF8都给出了一个字符串作为回报,它就不是一个解决方案。例如:UTF8给了我一些额外的字符(否定结果),ASCII显示正确的字符(正面结果)。我现在使用以下代码:
NSString *responseString, *responseStringASCII, *responseStringUTF8;
responseStringASCII = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
if (!responseStringASCII)
{
// ASCII is not working, will try utf-8!
responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
}
else
{
// ASCII is working, but check if UTF8 gives less characters
responseStringUTF8 = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
if(responseStringUTF8 != nil && [responseStringUTF8 length] < [responseStringASCII length])
{
responseString = [responseStringUTF8 retain];
}
else
{
responseString = [responseStringASCII retain];
}
[responseStringUTF8 release];
}
[responseStringASCII release];