NSData to NSString converstion problem!

别等时光非礼了梦想. 提交于 2019-12-20 15:49:55

问题


I'm getting an html file as NSData and need to parse it to extract some info. My approach was to convert it to NSString with UTF8 encoding (the html has non english characters, russian for example) - it failed. I used something like that:

NSString *respData = [NSString stringWithUTF8String:[theData bytes]];

but it returned nil.

The only thing that actually worked was

[NSString stringWithCString:[theData bytes] length:[theData length]];

but when it encounters russian characters for example it returns jibrish.

Then my next approach was to parse the byte array of the data, extract the bytes I need and somehow convert them to NSString. I tried something like that:

-(NSString *)UTF8StringFromData:(NSData *)theData{
 Byte *arr = [theData bytes];
 NSUInteger begin1 = [self findIndexOf:@"<li>" bArr:arr size:[theData length]]+4;
 NSUInteger end1 = [self findIndexOf:@"</li></ol>" bArr:arr size:[theData length]];
 Byte *arr1 = (Byte *)malloc(sizeof(Byte)*((end1-begin1+1)));
 int j = 0;
 for (int i = begin1; i < end1; i++){
  arr1[j] = arr[i];
  j++;
 }
 arr1[j]='\0';
 NSData *temp = [NSData dataWithBytes:arr1 length:j];
 return [[NSString alloc] initWithData:temp encoding:NSUTF8StringEncoding];
}

回答1:


Supposing you got a NSURLResponse* response and an NSData* data:

CFStringEncoding cfEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef) [response textEncodingName]);
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);

NSString* string = [[NSString alloc] initWithData:data encoding:encoding];

// Do stuff here..

[string release];



回答2:


I'm responding to the Martijn Thé thread above, here, as I couldn't put a readable code snippet in the comments.

I found that if on the server , the response content type is set to 'text/plain', then (__bridge CFStringRef) [response textEncodingName] will be null, and if you try to pass this to CFStringConvertIANACharSetNameToEncoding you will get an EXC_BAD_ACCESS signal.

If the content type of the response is set to 'text/html; charset=utf-8', then everything works as expected. To handle the 'text/plain' content type, this is what I did:

CFStringRef sRef = (__bridge CFStringRef)[response textEncodingName];
if (sRef)
{
        CFStringEncoding cfEncoding = CFStringConvertIANACharSetNameToEncoding(sRef);
        encoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);
}
else
{
        encoding = NSASCIIStringEncoding;
}




回答3:


First of all here is my code

-(NSString *)UTF8StringFromData:(NSData *)theData{
    Byte *arr = [theData bytes];
    NSUInteger begin1 = [self findIndexOf:@"<li>" bArr:arr size:[theData length]]+4;
    NSUInteger end1 = [self findIndexOf:@"</li></ol>" bArr:arr size:[theData length]];
    Byte *arr1 = (Byte *)malloc(sizeof(Byte)*((end1-begin1+1)));
    int j = 0;
    for (int i = begin1; i < end1; i++){
        arr1[j] = arr[i];
        j++;
    }
    arr1[j]='\0';
    NSData *temp = [NSData dataWithBytes:arr1 length:j];
    return [[NSString alloc] initWithData:temp encoding:NSUTF8StringEncoding];  
}

and second - I am getting the file contents from the web - so I can't be sure about anything. It's an html of a google translation if it helps...



来源:https://stackoverflow.com/questions/1409537/nsdata-to-nsstring-converstion-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!