Search code examples
iosencodingutf-8asciinsdata

Check if NSData contains ASCII or UTF8 Encoding


I am retrieving HTML, containing UTF8 or ASCII encoded text. For most common use it is ASCII decoding that works to display the text right:

NSString *responseString    =   [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];

Now I have another HTML page with UTF8 encoding, so I have to use:

NSString *responseString    =   [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

What kind of encoding I retrieve is random when loading websites. My question is, is there a way to check the NSData for what kind of decoding is the right to use? So I know which encoding type I need to use.

Thnx!


Solution

  • I don't know if it is possible to check the encoding of NSData so this is what I did:

    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