Created
April 21, 2013 10:55
-
-
Save rlindsgaard/5429242 to your computer and use it in GitHub Desktop.
Reverse the bytes of a NSData object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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