Skip to content

Instantly share code, notes, and snippets.

@fbatista
Last active December 10, 2015 21:28
Show Gist options
  • Save fbatista/4495149 to your computer and use it in GitHub Desktop.
Save fbatista/4495149 to your computer and use it in GitHub Desktop.
Convert NSData to a string of bits : "001100111001110011" ... Useful for debugging
//.h
@interface NSData (BinaryString)
- (NSString*)toBinaryString;
@end
//.m
@implementation NSData (BinaryString)
- (NSString*)toBinaryString
{
NSMutableString* output = @"";
for (int i = 0; i < [self length]; i++) {
for (int j = 7; j >= 0; j--) {
output = [output appendFormat:@"%d", ((((const unsigned char*)[self bytes])[i] >> j) & 0x1)];
}
}
return output;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment