简体   繁体   English

如何将NSString溢出到多个八位字节中

[英]How do I spilt a NSString into a number of octets

I want to split a NSString into NSStrings that have no more than 75 octets (in UTF8 representation) each. 我想将NSString拆分为每个NSString不超过75个八位字节(以UTF8表示)。

If my NSString would be completely ascii it would be a no-brainer. 如果我的NSString完全是ascii,那将是一件容易的事。

But since the resulting string could have any length between 18 and 75 characters I have no real idea how to do it. 但是由于结果字符串的长度在18到75个字符之间,所以我不知道该怎么做。

what's the way to do this? 这是怎么做的?

Convert the string into octets, take the first 75, convert it back to NSString and hope that NSString tells me that I've ripped an utf8-character into two parts? 将字符串转换为八位字节,取前75个字符串,将其转换回NSString,并希望NSString告诉我将utf8字符分为两部分?

the answer of warren showed me that I was on the right track. 沃伦(Warren)的回答告诉我,我的工作方向正确。 This is what I came up with: 这是我想出的:

- (NSString *)foldString:(NSString *)string withOctetCount:(NSInteger)octetCount {
    NSMutableString *output = [NSMutableString string];
    NSData *stringData = [string dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    NSInteger convertedBytes = 0;
    while (convertedBytes < [stringData length]) {
        NSString *subString = nil;
        NSInteger usedLength = octetCount;
        if (convertedBytes > 0) {
            // all lines after the first one get a space as prefix. so use one octet less in those lines
            usedLength--;
        }
        if (convertedBytes + usedLength > [stringData length]) {
            usedLength = [stringData length] - convertedBytes;
        }
        while (!subString) {
            NSData *data = [stringData subdataWithRange:NSMakeRange(convertedBytes, usedLength)];
            subString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
            if (!subString) {
                usedLength--;
                if (usedLength == 0) {
                    // TODO: remove abort
                    abort();
                    return nil;
                }
            }
        }
        //      NSLog(@"Used %d octets", usedLength);
        if (convertedBytes == 0) {
            // Dont prefix with space on first line
            [output appendString:subString];
        }
        else {
            [output appendFormat:@"\r\n %@", subString];
        }
        convertedBytes += usedLength;
    }
    return output;
}

NSString has NSString

- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding allowLossyConversion:(BOOL)flag

gives a unterminated C string but allows loss of chars 给出一个未终止的C字符串,但允许丢失字符

- (const char *)cStringUsingEncoding:(NSStringEncoding)encoding

will attempt to give you a C string but barf to NULL if it cant do it losslessly 会尝试给你一个C字符串,但是如果不能无损地将barf设置为NULL

which could be a way to detect that you are about to break up a pair. 这可能是一种检测到您即将分手的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM