Что-то вроде этого должно работать:
// Change TEST to the absolute path of the file to read the data from
NSString *fileName = @"TEST";
NSMutableData *data = [NSMutableData dataWithContentsOfFile:fileName];
if (data == nil || [data length] == 0) {
return;
}
Ваш оригинальный вопрос:
char bytes[] = {0x66, 0x61, 0x63};
char new_bytes[] = {0x61, 0x72, 0x73};
NSRange range = [data rangeOfData:[NSData dataWithBytes:bytes length:3] options:0 range:NSMakeRange(0, [data length])];
if (range.location == NSNotFound) {
return;
}
[data replaceBytesInRange:range withBytes:new_bytes];
[data writeToFile:fileName atomically:YES];
Отредактировано для подстановочного знака (я только использовал -1 для подстановочного знака, и это может бытьв любом месте):
char bytes[] = {0x66, -0x1, 0x63};
char new_bytes[] = {0x61, 0x72, 0x73};
NSUInteger dataLength = [data length];
if (dataLength < 3) {
return;
}
char *data_bytes = (char *)[data bytes];
int dataLengthMinus3 = dataLength - 3;
for (NSUInteger i = 0; i < dataLengthMinus3; i++) {
if (bytes[0] == -0x1 || data_bytes[i] == bytes[0]) {
if (bytes[1] == -0x1 || data_bytes[i+1] == bytes[1]) {
if (bytes[2] == -0x1 || data_bytes[i+2] == bytes[2]) {
for (int z = 0; z < 3; z++) {
if (new_bytes[z] != -0x1){
[data replaceBytesInRange:NSMakeRange(i+z, 1) withBytes:new_bytes[z]];
}
}
i += 2;
continue; // Make this break if you only want to replace the first occurrence.
}
}
}
}