核心代码:
NSString分类
/**
十转三十六
*/
+ (NSString * _Nonnull)tripleHexForInteger:(NSString * _Nonnull)integer {
NSString *regex = @"^[0-9]*$";
BOOL flag = [integer matchesRegex:regex];
if (!flag) {
return @"0";
}
NSDecimalNumber *num = NSDecimalNumberFromString(integer);
NSDecimalNumber *zero = NSDecimalNumberFromString(@"0");
NSMutableString *tripleHexString = @"".mutableCopy;
NSString *parma = @"0123456789abcdefghijklmnopqrstuvwxyz";
NSDecimalNumber *thirtySix = NSDecimalNumberFromString(@"36");
while ([num compare:zero] == NSOrderedDescending) {
NSDecimalNumber *remainder = [num bpModularBy:thirtySix];
num = [num bpDividingBy:thirtySix withBehavior:BPZeroDownDecimalNumberHandler];
NSRange range = NSMakeRange(remainder.unsignedIntegerValue, 1);
NSString *cc = [parma substringWithRange:range];
[tripleHexString insertString:cc atIndex:0];
}
return tripleHexString.copy;
}
/**
三十六转十
*/
+ (NSString * _Nonnull)integerForTripleHex:(NSString * _Nonnull)tripleHex {
NSString *regex = @"^[0-9a-z]*$";
BOOL flag = [tripleHex matchesRegex:regex];
if (!flag) {
return @"0";
}
NSString *str36 = tripleHex;
NSString *param = @"0123456789abcdefghijklmnopqrstuvwxyz";
NSDecimalNumber *sum = NSDecimalNumberFromString(@"0");
NSDecimalNumber *thirtySix = NSDecimalNumberFromString(@"36");
for (NSUInteger index = str36.length; index > 0; index--) {
NSRange range1 = NSMakeRange(index - 1, 1);
NSString *str = [str36 substringWithRange:range1];
NSUInteger location = [param rangeOfString:str].location;
NSDecimalNumber *index2 = [[NSDecimalNumber alloc] initWithUnsignedInteger:location];
NSUInteger power = str36.length - index;
NSDecimalNumber *multiple = [thirtySix decimalNumberByRaisingToPower:power];
NSDecimalNumber *result = [index2 bpMultiplyingBy:multiple];
sum = [sum bpAdding:result];
}
NSNumberFormatter *formatter = [NSNumberFormatter new];
formatter.maximumIntegerDigits = NSUIntegerMax;
formatter.minimumIntegerDigits = 1;
formatter.maximumFractionDigits = NSUIntegerMax;
formatter.minimumFractionDigits = 0;
return [formatter stringFromNumber:sum];
}
下面都是辅助代码:
NSDecimalNumber分类
- (NSDecimalNumber * _Nonnull)bpAdding:(NSDecimalNumber * _Nullable)decimalNumber {
return [self bpAdding:decimalNumber withBehavior:nil];
}
- (NSDecimalNumber * _Nonnull)bpSubtracting:(NSDecimalNumber * _Nullable)decimalNumber {
return [self bpSubtracting:decimalNumber withBehavior:nil];
}
- (NSDecimalNumber * _Nonnull)bpMultiplyingBy:(NSDecimalNumber * _Nullable)decimalNumber {
return [self bpMultiplyingBy:decimalNumber withBehavior:nil];
}
- (NSDecimalNumber * _Nonnull)bpDividingBy:(NSDecimalNumber * _Nullable)decimalNumber {
return [self bpDividingBy:decimalNumber withBehavior:nil];
}
/**
模运算
*/
- (NSDecimalNumber * _Nonnull)bpModularBy:(NSDecimalNumber * _Nullable)decimalNumber {
NSDecimalNumber *zero = NSDecimalNumberFromString(@"0");
NSDecimalNumber *intDivisor = [decimalNumber decimalNumberByRoundingAccordingToBehavior:BPZeroDownDecimalNumberHandler];
if ([intDivisor compare:zero] != NSOrderedDescending) {
return zero;
}
NSDecimalNumber *intSelf = [self decimalNumberByRoundingAccordingToBehavior:BPZeroDownDecimalNumberHandler];
NSDecimalNumber *multi = [intSelf bpDividingBy:intDivisor withBehavior:BPZeroDownDecimalNumberHandler];
NSDecimalNumber *sum = [multi bpMultiplyingBy:intDivisor];
return [intSelf bpSubtracting:sum];
}
- (NSDecimalNumber * _Nonnull)bpAdding:(NSDecimalNumber * _Nullable)decimalNumber withBehavior:(id _Nullable)behavior {
if (behavior) {
if (decimalNumber) {
return [self decimalNumberByAdding:decimalNumber withBehavior:behavior];
} else {
return [self decimalNumberByAdding:NSDecimalNumberFromString(@"0") withBehavior:behavior];
}
} else {
if (decimalNumber) {
return [self decimalNumberByAdding:decimalNumber];
} else {
return [self decimalNumberByAdding:NSDecimalNumberFromString(@"0")];
}
}
}
- (NSDecimalNumber * _Nonnull)bpSubtracting:(NSDecimalNumber * _Nullable)decimalNumber withBehavior:(id _Nullable)behavior {
if (behavior) {
if (decimalNumber) {
return [self decimalNumberBySubtracting:decimalNumber withBehavior:behavior];
} else {
return [self decimalNumberBySubtracting:NSDecimalNumberFromString(@"0") withBehavior:behavior];
}
} else {
if (decimalNumber) {
return [self decimalNumberBySubtracting:decimalNumber];
} else {
return [self decimalNumberBySubtracting:NSDecimalNumberFromString(@"0")];
}
}
}
- (NSDecimalNumber * _Nonnull)bpMultiplyingBy:(NSDecimalNumber * _Nullable)decimalNumber withBehavior:(id _Nullable)behavior {
if (behavior) {
if (decimalNumber) {
return [self decimalNumberByMultiplyingBy:decimalNumber withBehavior:behavior];
} else {
return [self decimalNumberByMultiplyingBy:NSDecimalNumberFromString(@"0") withBehavior:behavior];
}
} else {
if (decimalNumber) {
return [self decimalNumberByMultiplyingBy:decimalNumber];
} else {
return [self decimalNumberByMultiplyingBy:NSDecimalNumberFromString(@"0")];
}
}
}
- (NSDecimalNumber * _Nonnull)bpDividingBy:(NSDecimalNumber * _Nullable)decimalNumber withBehavior:(id _Nullable)behavior {
if (decimalNumber) {
if ([decimalNumber compare:NSDecimalNumberFromString(@"0")] == NSOrderedSame) {
return NSDecimalNumberFromString(@"0");
} else if (behavior) {
return [self decimalNumberByDividingBy:decimalNumber withBehavior:behavior];
} else {
return [self decimalNumberByDividingBy:decimalNumber];
}
} else {
return NSDecimalNumberFromString(@"0");
}
}
内联函数
static inline NSDecimalNumber * _Nonnull NSDecimalNumberFromString(NSString * _Nullable string) {
NSString *theString;
if (string.isRealNumber) {
theString = string;
} else {
theString = @"0";
}
return [NSDecimalNumber decimalNumberWithString:theString];
}
NSString分类
- (BOOL)matchesRegex:(NSString *)regex {
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [pred evaluateWithObject:self];
}
- (BOOL)isRealNumber {
NSString *regex = @"^(|-)(((\\d.\\d)|(\\d))|((\\d.\\d)|\\d)+((e|E)(|-)\\d)){1,9999}$";
return [self matchesRegex:regex];
}