NSData to NSString converstion problem!

前端 未结 3 1676
囚心锁ツ
囚心锁ツ 2021-02-06 11:34

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,

3条回答
  •  遥遥无期
    2021-02-06 12:28

    First of all here is my code

    -(NSString *)UTF8StringFromData:(NSData *)theData{
        Byte *arr = [theData bytes];
        NSUInteger begin1 = [self findIndexOf:@"
  • " bArr:arr size:[theData length]]+4; NSUInteger end1 = [self findIndexOf:@"
  • " 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...

提交回复
热议问题