我想添加新的NSdata = x20(ASCII),这是NSMutableData中最后一个空格,足够固定的byes。示例:NSmuatabledata有400字节,但是我需要这个NSMutableData有600字节。所以我想最后添加nsbyableData 200 byby(十六进制中的x20)。我怎样才能做到这一点?请帮我。提前致谢

// write xmlDoc to file
NSData* xmlData = [xmlDoc XMLDataWithOptions:NSXMLNodePreserveAll];
NSLog(@"xmlData %@",xmlData);
NSMutableData * datawrite = [xmlData mutableCopy ] ;
NSArray *array = [[NSArray alloc] init]; // i dont know how to add x20 (hex) to this array
[datawrite appendData:[[NSString stringWithFormat:@"%@", array] dataUsingEncoding:NSUTF8StringEncoding]];

最佳答案

NSMutableData *datawrite = ...
NSUInteger oldLength = [datawrite length];
NSUInteger newLength = 600;
if (newLength > oldLength ) {
    // extend mutable data object:
    [datawrite setLength:newLength];

    // fill appended bytes with space characters:
    char *bytes = [datawrite mutableBytes];
    memset(bytes + oldLength, ' ', newLength - oldLength);
}

08-16 03:41