Converting NSData to NString Problem

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];

}

+5
source share
4 answers

I know this is an old topic, but it appeared when I was looking for a solution today. I solved it now, so I just post it for others who may come across this page looking for a solution.

Here is what I do in an asynchronous request:

First I save the name of the text encoding in the connection: didReceiveResponse using

encodingName = [[NSString alloc] initWithString:[response textEncodingName]];

Then in my connection, the DidFinishLoading method used

NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef) encodingName));
NSString *payloadAsString = [[NSString alloc] initWithData:receivedData encoding:encoding];
+6
source

Gordon - NSData, :

    NSData *theData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];

, , ,

[[NSString alloc] initWithData:temp encoding:NSUTF8StringEncoding]

nil

To Ed - ( NSData, , , - - NSData, NSString... ...)

-(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];

}

0

charset = HTTP / ? , UTF-8.

0

, , , NSData.

-(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 = arr + begin1;
  NSData *temp = [NSData dataWithBytes:arr1 length:end1 - begin1];
  return [[NSString alloc] initWithData:temp encoding:NSUTF8StringEncoding];
}

, . (arr1). , GDB :

print (char *)arr1

, . ( , , . ).

, , - , , begin1 end1.

0

All Articles