答案 0 :(得分:20)
答案 1 :(得分:-1)
答案 2 :(得分:-1)
[此代码显然不需要解释]
(void)convertBinaryToNumeric{
NSString *bin_Input = @"1010";
NSString *reverseInput = [self reverseString:bin_Input];
int dec_Output = 0;
int dec_Counter = 0;
for (int i=0; i<[reverseInput length]; i++) {
dec_Output = [[reverseInput substringWithRange:NSMakeRange(i, 1)] intValue] * [self multipliesByTwo:i];
dec_Output = dec_Counter + dec_Output;
dec_Counter = dec_Output;
}
NSLog(@"Bin:%@ Dec:%d",bin_Input,dec_Output);
}
(NSString *)reverseString:(NSString *)inputStr{
NSMutableString *revStr = [[NSMutableString alloc]initWithString:@""];
for (int i=[inputStr length]-1; i>=0; i--) {
[revStr appendString:[inputStr substringWithRange:NSMakeRange(i, 1)]];
}
return revStr;
}
(int)multipliesByTwo:(int)number{
if(number == 0){
return 1;
}else{
int bin_value = 2;
int multipleBy=0;
for (int i=1; i<=number; i++) {
if(multipleBy == 0){
multipleBy = bin_value;
}else{
multipleBy = multipleBy *2;
}
}
return multipleBy;
}
return 0;
}