ios. 31进制转2进制

//31进制转2进制
+ (NSString *)convertBinarySystemFromDecimalSystem:(NSString *)keyString 
{
    unsigned long  num = strtoul([keyString UTF8String], 0, 31); //利用函数strtoul(const char *, char **, int),通过对应的进制格式,将字符串转换成十进制
    NSInteger remainder = 0;      //余数
    NSInteger divisor = 0;        //除数
    NSString * prepare = @"";
    while (true){
        remainder = num%2;
        divisor = num/2;
        num = divisor;
        prepare = [prepare stringByAppendingFormat:@"%ld",(long)remainder];
        if (divisor == 0){
            break;
        }
    }
    NSString * result = @"";
    for (NSInteger i = prepare.length - 1; i >= 0; i --){
        result = [result stringByAppendingFormat:@"%@",
                  [prepare substringWithRange:NSMakeRange(i , 1)]];
    }
    return result;
}

你可能感兴趣的:(ios. 31进制转2进制)