1

When I use [[NSString alloc] initWithData:subdata encoding:encoding] method , I find the string is incomplete. Why? Any restrictions in NSString?

Then,I suspect that the data is too large,so I create a new method to Transfer.

-(NSString *)stringFromData:(NSData *)data encoding:(NSStringEncoding)encoding{
    NSInteger DataLen = data.length;
    NSInteger segLen = 5000;
    NSInteger times = DataLen/segLen;
    NSInteger fristLen = DataLen%segLen;
    NSMutableString *str = [NSMutableString string];
    [str appendString:[[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, fristLen)] encoding:encoding]];
    NSLog(@"%@",str);
    while (times--) {
        NSData *subdata = [data subdataWithRange:NSMakeRange(fristLen, segLen)] ;
        if (subdata) {
            NSString*substr = [[NSString alloc] initWithData:subdata encoding:encoding]; //tag1
            NSLog(@"%@",substr);
            if (substr) {
               [str appendString:substr];
            }
            fristLen += segLen;
        }else{
            break;
        }
    }
    return str;
}

but in tag1, I find the string is null in some conditions. What is the problem?

pkamb
  • 33,281
  • 23
  • 160
  • 191
  • 3
    Can you tell us what data you are passing in and what encoding you are using? There are several restrictions on various encodings, so depending which one you are using, if your data isn't actually valid text, that might be an issue. Also, while NSString can have text that contains the '\0' character, NSLog() can not, so you may be getting the right string, your log may just not show it. NSString is *only* intended for use with text. You shouldn't pass any old binary data in. – uliwitness Apr 23 '17 at 09:20
  • Possible duplicate of [Convert NSData to NSString - NSLog limitation](http://stackoverflow.com/questions/41914815/convert-nsdata-to-nsstring-nslog-limitation) – koen Apr 23 '17 at 13:55
  • Thanks for your answer , finally I found The string contains special characters; on the other hand, I found NSLog just print a certain number of strings. – user6277122 Apr 26 '17 at 11:50

0 Answers0