char * CharToBinary( const char input )
{
char binary[] = "00000000"; //< bug, since this will be deallocated on return.
if ( input & 0x80 )
binary[0] = '1';
if ( input & 0x40 )
binary[1] = '1';
if ( input & 0x20 )
binary[2] = '1';
if ( input & 0x10 )
binary[3] = '1';
if ( input & 0x08 )
binary[4] = '1';
if ( input & 0x04 )
binary[5] = '1';
if ( input & 0x02 )
binary[6] = '1';
if ( input & 0x01 )
binary[7] = '1';
return binary;
}
-(NSString*)binaryStringRepresentation:(NSInteger)input drawBoxes:(BOOL)shouldDrawBoxes {
NSMutableString *s = [NSMutableString string];
NSInteger i = 0;
NSInteger mask = 0;
NSInteger bits;
#if __LP64__
bits = 64;
#else
bits = 32;
#endif
if (shouldDrawBoxes) {
for (i = 0; i < bits / 8; i++) {
[s appendString:@"+--------"];
}
[s appendString:@"+\n"];
}
for (i = 0; i < bits; i++) {
if (i % 8 == 0) {
if (shouldDrawBoxes) {
[s appendString:@"|"];
} else {
if (i > 1) {
[s appendString:@" "];
}
}
}
mask = 0x1 << ((bits - 1) - i);
if (input & mask) {
[s appendString:@"1"];
} else {
[s appendString:@"0"];
}
}
if (shouldDrawBoxes) {
[s appendString:@"|\n"];
for (i = 0; i < bits / 8; i++) {
[s appendString:@"+--------"];
}
[s appendString:@"+\n"];
}
return s;
}
+--------+--------+--------+--------+
|00100010|10100101|11110000|00001111|
+--------+--------+--------+--------+
Wouldn't it be better to use sizeof(input) * 8?Code:#if __LP64__ bits = 64; #else bits = 32; #endif
![]()
- (NSString *)bits:(NSInteger)value forSize:(int)size {
const int shift = 8*size - 1;
const unsigned mask = 1 << shift;
NSMutableString *result = [NSMutableString string];
for (int i = 1; i <= shift + 1; i++ ) {
[result appendString:(value & mask ? @"1" : @"0")];
value <<= 1;
if (i % 8 == 0) {
[result appendString:@" "];
}
}
return result;
}
- (NSString *)bitsForInteger:(NSInteger)value {
return [self bits:value forSize:sizeof(NSInteger)];
}
- (NSString *)bitsForString:(NSString *)value {
const char *cString = [value UTF8String];
int length = strlen(cString);
NSMutableString *result = [NSMutableString string];
for (int i = 0; i < length; i++) {
[result appendString:[self bits:*cString++ forSize:sizeof(char)]];
}
return result;
}
Very nice function and it calculates the correct value
I use NSInteger bits = sizeof(input)*8; instead of
NSInteger bits;
#if __LP64__
bits = 64;
#else
bits = 32;
#endif
Thank you!
NSInteger number = 0xAAAA;
NSUInteger bit = ~(NSUIntegerMax >> 1);
do
{
printf("%c", (((NSUInteger)number & bit) ? '1' : '0'));
} while ( bit >>= 1 );
#import <Cocoa/Cocoa.h>
NSString* stringCreateWithNSInteger(NSInteger number)
{
NSMutableString* string = [[NSMutableString string] retain];
NSUInteger bit = ~(NSUIntegerMax >> 1);
do
{
[string appendString:(((NSUInteger)number & bit) ? @"1" : @"0")];
} while ( bit >>= 1 );
return string;
}
int main(int argc, const char* argv[])
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSInteger number = 0xDEADBEEF;
NSString* string = stringCreateWithNSInteger(number);
NSLog(@"%X = %@", number, string);
[string release];
[pool release];
return 0;
}