This convenient method converts an integer to a binary number.
/** Convert an int to a binary.
@param val Integer value.
@return Returns the binary value as string.
*/
+ (NSString *)binaryFromInt:(NSInteger)val {
NSMutableString *str = [NSMutableString string];
for (NSInteger num = val; num > 0; num >>= 1) {
[str insertString:((num & 1) ? @"1" : @"0") atIndex:0]; // Prepend 0 or 1, depending on the bit.
}
return str;
}
Download the ready-for-use source file (.m) of Int to binary
No comments:
Post a Comment