I get the html file as NSData and have to parse it in order to extract some information. My approach was to convert it to NSString with UTF8 encoding (for example, html has non-English characters, for example, Russian) - this failed. I used something like this:
NSString *respData = [NSString stringWithUTF8String:[theData bytes]];
but he returned zero.
The only thing that actually worked was
[NSString stringWithCString:[theData bytes] length:[theData length]];
but when he encounters Russian characters, for example, he returns jibrish.
Then my next approach was to parse an array of data bytes, extract the bytes I needed, and somehow convert them to NSString. I tried something like this:
-(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]; }
objective-c iphone encoding nsstring nsdata
Alex1987
source share