美文网首页
iOS 进制转换

iOS 进制转换

作者: 浮浅丶Superficial | 来源:发表于2018-06-04 18:33 被阅读12次

    首先了解系统方法

    /**
     C字符串转无符号长整型
    
     @param __str C字符串
     @param __endptr 对类型为 char* 的对象的引用,其值为__str停止转换的下一个字符
     @param __base C字符串的进制 2~36
     @return 无符号长整型
     */
    unsigned long  strtoul(const char *__str, char **__endptr, int __base);
    

    strtoul 栗子

    // 二进制字符串转整型
    NSString *binary = @"1101";
    char *endpth;
    NSUInteger decimal = strtoul([binary UTF8String], &endpth, 2);
    NSLog(@"decimal:%li, endpth:%@", decimal, [NSString stringWithUTF8String:endpth]);
    输出:
    2018-06-04 17:01:44.836182+0800 ZXXTestDemo[30806:2638721] decimal:13, endpth:
    
    // 十六进制字符串转整型
    NSString *hex = @"0X1A";
    char *endpth;
    NSUInteger decimal = strtoul([hex UTF8String], &endpth, 16);
    NSLog(@"decimal:%li, endpth:%@", decimal, [NSString stringWithUTF8String:endpth]);
    输出:
    2018-06-04 17:07:38.029495+0800 ZXXTestDemo[31055:2666063] decimal:26, endpth:
    
    // 字符串有误时, 可以根据 endpth 进行判断
    NSString *binary = @"1121";
    char *endpth;
    NSUInteger decimal = strtoul([binary UTF8String], &endpth, 2);
    NSLog(@"decimal:%li, endpth:%@", decimal, [NSString stringWithUTF8String:endpth]);
    输出:
    2018-06-04 17:08:11.607153+0800 ZXXTestDemo[30940:2653669] decimal:3, endpth:21
    

    但是居然没有与 strtoul 对应的 ultostr 方法, so 就自己写了一个类似的方法

    /**
     无符号长整型转C字符串
    
     @param num 无符号长整型
     @param base 进制 2~36
     @return C字符串
     */
    char *ultostr(unsigned long num, unsigned base) {
        static char string[64] = {'\0'};
        size_t max_chars = 64;
        char remainder;
        int sign = 0;
        if (base < 2 || base > 36) {
            return NULL;
        }
        for (max_chars --; max_chars > sign && num != 0; max_chars --) {
            remainder = (char)(num % base);
            if ( remainder <= 9 ) {
                string[max_chars] = remainder + '0';
            } else {
                string[max_chars] = remainder - 10 + 'A';
            }
            num /= base;
        }
        if (max_chars > 0) {
            memset(string, '\0', max_chars + 1);
        }
        return string + max_chars + 1;
    }
    

    ultostr 栗子

    // 整型转二进制
    NSUInteger decimal = 15;
    char *binary = ultostr(decimal, 2);
    NSLog(@"binary:%@", [NSString stringWithUTF8String:binary ? : "\0"]);
    输出:
    2018-06-04 17:25:46.674484+0800 ZXXTestDemo[31246:2693033] binary:1111
    
    NSUInteger decimal = 15;
    char *hex = ultostr(decimal, 16);
    NSLog(@"hex:%@", [NSString stringWithUTF8String:hex ? : "\0"]);
    输出:
    2018-06-04 17:28:29.335201+0800 ZXXTestDemo[31318:2700423] hex:F
    

    strtoul:任意进制的字符串可以转整型, ultostr:整型又可以转任意进制字符串, 那么进制间的转换就 so easy

    以下是常用的进制转换

    @interface NSString (ZXXExtension)
    
    /// 十进制转二进制
    + (NSString *)getBinaryByDecimal:(NSInteger)decimal;
    /// 二进制转十进制
    + (NSInteger)getDecimalByBinary:(NSString *)binary;
    /// 十六进制转十进制
    + (NSInteger)getDecimalByHex:(NSString *)hex;
    /// 十进制转十六进制
    + (NSString *)getHexByDecimal:(NSInteger)decimal;
    /// 十六进制转二进制
    + (NSString *)getBinaryByHex:(NSString *)hex;
    /// 二进制转十六进制
    + (NSString *)getHexByBinary:(NSString *)binary;
    
    @end
    
    #import "NSString+ZXXExtension.h"
    
    @implementation NSString (ZXXExtension)
    
    /**
     无符号长整型转C字符串
     
     @param num 无符号长整型
     @param base 进制 2~36
     @return C字符串
     */
    char *ultostr(unsigned long num, unsigned base) {
        static char string[64] = {'\0'};
        size_t max_chars = 64;
        char remainder;
        int sign = 0;
        if (base < 2 || base > 36) {
            return NULL;
        }
        for (max_chars --; max_chars > sign && num != 0; max_chars --) {
            remainder = (char)(num % base);
            if ( remainder <= 9 ) {
                string[max_chars] = remainder + '0';
            } else {
                string[max_chars] = remainder - 10 + 'A';
            }
            num /= base;
        }
        if (max_chars > 0) {
            memset(string, '\0', max_chars + 1);
        }
        return string + max_chars + 1;
    }
    
    /// 十进制转二进制
    + (NSString *)getBinaryByDecimal:(NSInteger)decimal {
        char *hexChar = ultostr(decimal, 2);
        NSString *binary = [NSString stringWithUTF8String:hexChar];
        return binary;
    }
    
    /// 二进制转换为十进制
    + (NSInteger)getDecimalByBinary:(NSString *)binary {
        char *endptr = "";
        NSInteger decimal = strtoul([binary UTF8String], &endptr, 2);
        if (strlen(endptr)) {
            return 0;
        }
        return decimal;
    }
    
    /// 十六进制转换十进制
    + (NSInteger)getDecimalByHex:(NSString *)hex {
        char *endptr = "";
        NSInteger decimal = strtoul([hex UTF8String], &endptr, 16);
        if (strlen(endptr)) {
            return 0;
        }
        return decimal;
    }
    
    /// 十进制转换十六进制
    + (NSString *)getHexByDecimal:(NSInteger)decimal {
        char *hexChar = ultostr(decimal, 16);
        NSString *hex = [NSString stringWithUTF8String:hexChar];
        return hex;
    }
    
    /// 十六进制转换为二进制
    + (NSString *)getBinaryByHex:(NSString *)hex {
        // 十进制
        NSInteger decimal = [NSString getDecimalByHex:hex];
        // 二进制字符串
        NSString *binary = [NSString getBinaryByDecimal:decimal];
        return binary;
    }
    
    /// 二进制转换成十六进制
    + (NSString *)getHexByBinary:(NSString *)binary {
        // 十进制
        NSInteger decimal = [NSString getDecimalByBinary:binary];
        // 十六进制
        NSString *hex = [NSString getHexByDecimal:decimal];
        return hex;
    }
    
    @end
    

    GitHub 地址

    相关文章

      网友评论

          本文标题:iOS 进制转换

          本文链接:https://www.haomeiwen.com/subject/sksysftx.html