I am trying to get NSMutableArray from NSData by 16 bytes, but failing to do so. Maybe anyone could point me in the right direction.
Example:
input: <c4ebc39d edf81fe6 09e0a41a 34b4d20d c4ebc39d edf81fe6 09e0a41a 34b4d20d>
output: <c4ebc39d edf81fe6 09e0a41a 34b4d20d> <c4ebc39d edf81fe6 09e0a41a 34b4d20d>
I am trying like this:
NSMutableArray *blocks = [[NSMutableArray alloc]init];
for (NSUInteger i = 0; i <= [data length]; i=i+16) {
unsigned char *byte = NULL;
[data getBytes:byte range:NSMakeRange(i, 16)];
NSData *temp = [[NSData alloc] initWithBytes:byte length:sizeof(byte)];
[blocks addObject:temp];
}
For some reason this throws me exc_bad_access.
copies the data into the memory pointed to by
byte, it does not allocate memory. Copying the data crashes becausebyte == NULLdoes not point to valid memory. Replacingby
should solve the problem. But note that
is even easier and does not require the byte buffer at all.
Note also that the for loop
should be changed to
to avoid access to the
dataobject beyond its bounds.You loop would then look like this: