I get the HTML file as NSData and you need to extract some parts of it. To do this, I need to convert it to UTF8 encoded NSString. The fact is that this conversion fails, probably because NSData contains bytes that are not valid for UTF8. I tried to get an array of data bytes and step over it, but every time I come across an ASCII character (like Hebrew), I get jibrish.
Help will be appreciated.
UPDATE:
In Gordon, NSData is generated as follows:
NSData *theData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];
When I say that the conversion fails, I mean that
[[NSString alloc] initWithData:temp encoding:NSUTF8StringEncoding]
returns nil
To Ed - here is my code (I got an array of bytes from NSData, found what I need and built another byte array from this - turned it into NSData, and then tried to convert it to NSString ... sounds pretty complicated. ..)
-(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)));
NSLog(@"%d %d",begin1, end1);
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];
}
Alex1987
source
share