美文网首页iOS 开发iOS学习笔记2017-3-29iOS Developer
【iOS 开发】二进制、十进制、十六进制相互转换的方法

【iOS 开发】二进制、十进制、十六进制相互转换的方法

作者: 爱吃鸭梨的猫 | 来源:发表于2017-03-16 18:33 被阅读5427次
    Xcode

    下面罗列了二进制、十进制、十六进制之间相互转换的方法,有时候可能会用的到,需要的朋友可以收藏一下。


    1. 十进制转换为二进制

    /**
     十进制转换为二进制
      
     @param decimal 十进制数
     @return 二进制数
     */
    + (NSString *)getBinaryByDecimal:(NSInteger)decimal {
        
        NSString *binary = @"";
        while (decimal) {
            
            binary = [[NSString stringWithFormat:@"%ld", decimal % 2] stringByAppendingString:binary];
            if (decimal / 2 < 1) {
                
                break;
            }
            decimal = decimal / 2 ;
        }
        if (binary.length % 4 != 0) {
            
            NSMutableString *mStr = [[NSMutableString alloc]init];;
            for (int i = 0; i < 4 - binary.length % 4; i++) {
                
                [mStr appendString:@"0"];
            }
            binary = [mStr stringByAppendingString:binary];
        }
        return binary;
    }
    

    2. 十进制转换为十六进制

    /**
     十进制转换十六进制
      
     @param decimal 十进制数
     @return 十六进制数
     */
    + (NSString *)getHexByDecimal:(NSInteger)decimal {
        
        NSString *hex =@"";
        NSString *letter;
        NSInteger number;
        for (int i = 0; i<9; i++) {
            
            number = decimal % 16;
            decimal = decimal / 16;
            switch (number) {
                    
                case 10:
                    letter =@"A"; break;
                case 11:
                    letter =@"B"; break;
                case 12:
                    letter =@"C"; break;
                case 13:
                    letter =@"D"; break;
                case 14:
                    letter =@"E"; break;
                case 15:
                    letter =@"F"; break;
                default:
                    letter = [NSString stringWithFormat:@"%ld", number];
            }
            hex = [letter stringByAppendingString:hex];
            if (decimal == 0) {
                
                break;
            }
        }
        return hex;
    }
    

    3. 二进制转换为十六进制

    /**
     二进制转换成十六进制
       
     @param binary 二进制数
     @return 十六进制数
     */
    + (NSString *)getHexByBinary:(NSString *)binary {
        
        NSMutableDictionary *binaryDic = [[NSMutableDictionary alloc] initWithCapacity:16];
        [binaryDic setObject:@"0" forKey:@"0000"];
        [binaryDic setObject:@"1" forKey:@"0001"];
        [binaryDic setObject:@"2" forKey:@"0010"];
        [binaryDic setObject:@"3" forKey:@"0011"];
        [binaryDic setObject:@"4" forKey:@"0100"];
        [binaryDic setObject:@"5" forKey:@"0101"];
        [binaryDic setObject:@"6" forKey:@"0110"];
        [binaryDic setObject:@"7" forKey:@"0111"];
        [binaryDic setObject:@"8" forKey:@"1000"];
        [binaryDic setObject:@"9" forKey:@"1001"];
        [binaryDic setObject:@"A" forKey:@"1010"];
        [binaryDic setObject:@"B" forKey:@"1011"];
        [binaryDic setObject:@"C" forKey:@"1100"];
        [binaryDic setObject:@"D" forKey:@"1101"];
        [binaryDic setObject:@"E" forKey:@"1110"];
        [binaryDic setObject:@"F" forKey:@"1111"];
        
        if (binary.length % 4 != 0) {
            
            NSMutableString *mStr = [[NSMutableString alloc]init];;
            for (int i = 0; i < 4 - binary.length % 4; i++) {
                
                [mStr appendString:@"0"];
            }
            binary = [mStr stringByAppendingString:binary];
        }
        NSString *hex = @"";
        for (int i=0; i<binary.length; i+=4) {
            
            NSString *key = [binary substringWithRange:NSMakeRange(i, 4)];
            NSString *value = [binaryDic objectForKey:key];
            if (value) {
                
                hex = [hex stringByAppendingString:value];
            }
        }
        return hex;
    }
    

    4. 十六进制转换为二进制

    /**
     十六进制转换为二进制
       
     @param hex 十六进制数
     @return 二进制数
     */
    + (NSString *)getBinaryByHex:(NSString *)hex {
        
        NSMutableDictionary *hexDic = [[NSMutableDictionary alloc] initWithCapacity:16];
        [hexDic setObject:@"0000" forKey:@"0"];
        [hexDic setObject:@"0001" forKey:@"1"];
        [hexDic setObject:@"0010" forKey:@"2"];
        [hexDic setObject:@"0011" forKey:@"3"];
        [hexDic setObject:@"0100" forKey:@"4"];
        [hexDic setObject:@"0101" forKey:@"5"];
        [hexDic setObject:@"0110" forKey:@"6"];
        [hexDic setObject:@"0111" forKey:@"7"];
        [hexDic setObject:@"1000" forKey:@"8"];
        [hexDic setObject:@"1001" forKey:@"9"];
        [hexDic setObject:@"1010" forKey:@"A"];
        [hexDic setObject:@"1011" forKey:@"B"];
        [hexDic setObject:@"1100" forKey:@"C"];
        [hexDic setObject:@"1101" forKey:@"D"];
        [hexDic setObject:@"1110" forKey:@"E"];
        [hexDic setObject:@"1111" forKey:@"F"];
        
        NSString *binary = @"";
        for (int i=0; i<[hex length]; i++) {
            
            NSString *key = [hex substringWithRange:NSMakeRange(i, 1)];
            NSString *value = [hexDic objectForKey:key.uppercaseString];
            if (value) {
                
                binary = [binary stringByAppendingString:value];
            }
        }
        return binary;
    }
    

    5. 二进制转换为十进制

    /**
     二进制转换为十进制
      
     @param binary 二进制数
     @return 十进制数
     */
    + (NSInteger)getDecimalByBinary:(NSString *)binary {
        
        NSInteger decimal = 0;
        for (int i=0; i<binary.length; i++) {
            
            NSString *number = [binary substringWithRange:NSMakeRange(binary.length - i - 1, 1)];
            if ([number isEqualToString:@"1"]) {
                
                decimal += pow(2, i);
            }
        }
        return decimal;
    }
    

    大概就这么多了,有其他需求可以补充,需要十六进制转换为十进制的话,就按上面方法将十六进制转换为二进制后再转换为十进制就行了,有需要的朋友就收藏一下吧!

    将来的你,一定会感激现在拼命的自己,愿自己与读者的开发之路无限美好。

    我的传送门: 博客简书微博GitHub

    相关文章

      网友评论

      • H5:就是有一个小问题就是转换后十位数上的0会省略,应该是07结果是7
      • 爱吃鸭梨的猫:@RBNot其他的没问题吧,我明天看一下。
        爱吃鸭梨的猫:解决了,已经更新了。
        RBNote:http://www.jianshu.com/writer#/notebooks/12002499/notes/12491331/preview
        RBNote:我找到问题了. 问:0x0f 和0x0F 有区别吗?
        一:
        1.[hexDic objectForKey:key] 如果是小写字母的话,取出来的就是 nil
        2.然后字符串拼接一个 nil 参数,就会奔溃 报错信息如下:
        3.报错信息:-[__NSCFString stringByAppendingString:]: nil argument'

        二:解决办法
        将小写字母转为大写字母. key = key.uppercaseString;
        三:建议:请用NSMutableString ,为什么要用不可变字符串? 本来就是一个变化的字符串啊!

        三:建议
      • RBNote:有问题啊, 十六进制转二进制时候,程序崩溃了
      • RBNote:有用收藏

      本文标题:【iOS 开发】二进制、十进制、十六进制相互转换的方法

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