Skip to content

Instantly share code, notes, and snippets.

@rlindsgaard
Created April 21, 2013 10:55
Show Gist options
  • Save rlindsgaard/5429242 to your computer and use it in GitHub Desktop.
Save rlindsgaard/5429242 to your computer and use it in GitHub Desktop.
Reverse the bytes of a NSData object
- (NSData *)reverseData:(NSData *)data {
const char *bytes = [data bytes];
int idx = [data length] - 1;
char *reversedBytes = calloc(sizeof(char),[data length]);
for (int i = 0; i < [data length]; i++) {
reversedBytes[idx--] = bytes[i];
}
NSData *reversedData = [NSData dataWithBytes:reversedBytes length:[data length]];
free(reversedBytes);
return reversedData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment