首页 > 代码库 > 二进制通用公式
二进制通用公式
+ (NSString*)changeToBinarySystem:(NSString*)number {
NSInteger num = [number intValue];
NSInteger remainder = 0;
NSInteger divisor = 0;
NSString *prepare=@"";
while (num!=0) {
remainder = num%2;
divisor = num/2;
num = divisor;
prepare = [prepare stringByAppendingFormat:@"%ld",remainder];
}
NSString *result=@"";
int strLenht = prepare.length%4;
if (strLenht==1) {
result = @"000";
}else if (strLenht==2) {
result = @"00";
}else if (strLenht==3) {
result = @"0";
}
for (NSInteger i = prepare.length - 1; i >= 0;i--) {
result = [result stringByAppendingFormat:@"%@",[prepare substringWithRange:NSMakeRange(i, 1)]];
}
return result;
}
二进制通用公式