iOS-二进制、十进制、十六进制相互转化

2017-10-11  本文已影响0人  中秋梧桐语

//十进制转十六进制字符串

+  (NSString *)decimalToHexadecimal:(NSInteger)num{
    return [NSString stringWithFormat:@"%@",[[NSString alloc] initWithFormat:@"%.2lX",(long)num]];
}
+  (NSString *)decimalStringToHexadecimal:(NSString *)decimalString{
    
    if ([decimalString length] % 2 != 0) {
        return @"";
    }
    NSString *hexadecimalCount = [NSString string];
    for (NSInteger length = 0; length<decimalString.length; ) {
        
        NSString *hexadecimal = [NSString decimalToHexadecimal:[[decimalString substringWithRange:NSMakeRange(length, 2)] integerValue]];
        length += 2;
        
        hexadecimalCount = [hexadecimalCount stringByAppendingString:[NSString stringWithFormat:@"%@",hexadecimal]];
    }
    
    return hexadecimalCount;
}

//二进制转十进制

+ (NSString *)binaryToDecimal:(NSString *)decimal
{
    int ll = 0 ;
    int  temp = 0 ;
    for (int i = 0; i < decimal.length; i ++)
    {
        temp = [[decimal substringWithRange:NSMakeRange(i, 1)] intValue];
        temp = temp * powf(2, decimal.length - i - 1);
        ll += temp;
    }
    
    NSString * result = [NSString stringWithFormat:@"%d",ll];
    
    return result;
}

//十六进制转二进制

+ (NSString *)hexadecimalToBinary:(NSString *)hexadecimal
{
    NSMutableDictionary  *hexDic = [[NSMutableDictionary alloc] init];
    
    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"];
    
    NSMutableString *binaryString=[[NSMutableString alloc] init];
    
    for (int i=0; i<[hexadecimal length]; i++) {
        
        NSRange rage;
        
        rage.length = 1;
        
        rage.location = i;
        
        NSString *key = [hexadecimal substringWithRange:rage];
        
        //NSLog(@"%@",[NSString stringWithFormat:@"%@",[hexDic objectForKey:key]]);
        
        binaryString = [[NSString stringWithFormat:@"%@%@",binaryString,[NSString stringWithFormat:@"%@",[hexDic objectForKey:key]]] copy];
        
    }
    
    //NSLog(@"转化后的二进制为:%@",binaryString);
    
    return binaryString;
    
}

//十六进制转十进制

+ (NSString *)hexadecimalToDecimal:(NSString *)hexadecimal{
    
    NSString *decimal = [NSString stringWithFormat:@"%lu",strtoul([[hexadecimal substringWithRange:NSMakeRange(0, hexadecimal.length)] UTF8String], 0, 16)];
    return decimal;
}

// 十进制转二进制

+ (NSString *)decimalToBinary:(NSString *)binary
{
    NSInteger num = [binary integerValue];
    NSInteger remainder = 0;      //余数
    NSInteger divisor = 0;        //除数
    
    NSString * prepare = @"";
    
    while (true)
    {
        remainder = num%2;
        divisor = num/2;
        num = divisor;
        prepare = [prepare stringByAppendingFormat:@"%ld",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;
}

// 其他操作

+ (NSString *)stringToASCIICode:(NSString *)string{
    NSString *ASCIICode = [NSString string];
    for (NSInteger length = 0; length < string.length; length++) {
        NSString *first = [string substringWithRange:NSMakeRange(length, 1)];
        
        ASCIICode = [ASCIICode stringByAppendingString:[NSString decimalToHexadecimal:[first characterAtIndex:0]]];
    }
    
    return ASCIICode;
}

+ (NSString *)hexadecimalASCIICodeToString:(NSString *)hexString
{
    NSInteger length = 0;
    NSString *string = @"";
    while (length < hexString.length) {
        NSString *decimal = [NSString hexadecimalToDecimal:[hexString substringWithRange:NSMakeRange(length, 2)]];
        length += 2;
        string = [NSString stringWithFormat:@"%@%c",string,[decimal intValue]];
    }
    return string;
}

+ (NSString *)stringZeroPaddingbringToFront:(NSString *)string Length:(NSInteger)length{
    
    NSInteger needlength = length - [string length];
    while (needlength > 0) {
        needlength--;
        string = [NSString stringWithFormat:@"0%@",string];
    }
    
    return string;
    
}

+ (NSString *)afterStringZeroPadding:(NSString *)string Length:(NSInteger)length{
    NSInteger needlength = length - [string length];
    while (needlength > 0) {
        needlength--;
        string = [NSString stringWithFormat:@"%@0",string];
    }
    
    return string;
}
上一篇下一篇

猜你喜欢

热点阅读