Last active
December 10, 2015 21:28
-
-
Save fbatista/4495149 to your computer and use it in GitHub Desktop.
Convert NSData to a string of bits : "001100111001110011" ... Useful for debugging
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
//.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