Note that there are some explanatory texts on larger screens.

plurals
  1. PONSData to NString conversion problem
    primarykey
    data
    text
    <p>I'm getting an HTML file as NSData and need to extract some parts of it. For that I need to convert it to NSString with UTF8 encoding. The thing is that this conversion fails, probably because the NSData contains bytes that are invalid for UTF8. I have tried to get the byte array of the data and go over it, but each time I come across non ASCII character (hebrew letters for example) I get jibrish.</p> <p>Help will be appreciated.</p> <p>UPDATE:</p> <p>To Gordon - the NSData generated like that:</p> <pre><code> NSData *theData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&amp;theResponse error:&amp;theError]; </code></pre> <p>When I say that the conversion fails I mean that </p> <pre><code>[[NSString alloc] initWithData:temp encoding:NSUTF8StringEncoding] </code></pre> <p>returns nil</p> <p>To Ed - Here is my code (I got the Byte array from NSData, found what I need, and constructed another Byte array from that - turned it to NSData and then attempted to convert it to NSString... sounds kinda complicated...)</p> <pre><code>-(NSString *)UTF8StringFromData:(NSData *)theData{ Byte *arr = [theData bytes]; NSUInteger begin1 = [self findIndexOf:@"&lt;li&gt;" bArr:arr size:[theData length]]+4; NSUInteger end1 = [self findIndexOf:@"&lt;/li&gt;&lt;/ol&gt;" 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 &lt; end1; i++){ arr1[j] = arr[i]; j++; } arr1[j]='\0'; NSData *temp = [NSData dataWithBytes:arr1 length:j]; return [[NSString alloc] initWithData:temp encoding:NSUTF8StringEncoding]; </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload