美文网首页
进制转换

进制转换

作者: 马小悦 | 来源:发表于2016-12-09 11:49 被阅读48次

    1.十进制转二进制

    2.二进制转十进制

    3.十进制转十六进制

    4.十六进制转十进制

    5.二进制转十六进制

    6.十六进制转二进制

    //  十进制转二进制

    +(NSString*)toBinarySystemWithDecimalSystem:(NSString*)decimal

    {

    intnum=[decimal intValue];

    intremainder=0;//余数

    intdivisor=0;//除数

    NSString*prepare=@"";

    while(true)

    {

    remainder=num%2;

    divisor=num/2;

    num=divisor;

    prepare=[prepare stringByAppendingFormat:@"%d",remainder];

    if(divisor==0)

    {

    break;

    }

    }

    NSString*result=@"";

    for(inti=prepare.length-1;i>=0;i--)

    {

    result=[result stringByAppendingFormat:@"%@",

    [prepare substringWithRange:NSMakeRange(i,1)]];

    }

    returnresult;

    }

    //  二进制转十进制

    +(NSString*)toDecimalSystemWithBinarySystem:(NSString*)binary

    {

    intll=0;

    inttemp=0;

    for(inti=0;i

    {

    temp=[[binary substringWithRange:NSMakeRange(i,1)]intValue];

    temp=temp*powf(2,binary.length-i-1);

    ll+=temp;

    }

    NSString*result=[NSStringstringWithFormat:@"%d",ll];

    returnresult;

    }

    //十进制转十六进制

    NSString *hexString = [NSString stringWithFormat:@"%@",[[NSString alloc] initWithFormat:@"%1x",整形参数]];

    //十六进制转十进制

    UInt64 mac1 =  strtoul([@"abcd1234" UTF8String], 0, 16);

    //如果在有溢出,使用下面方法:

    unsigned long long result = 0;

    NSScanner *scanner = [NSScanner scannerWithString:@"abcd12345678"];

    [scanner scanHexLongLong:&result];

    //十六进制转二进制

    -(NSString *)getBinaryByhex:(NSString *)hex

    {

    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<[hex length]; i++) {

    NSRange rage;

    rage.length = 1;

    rage.location = i;

    NSString *key = [hex substringWithRange:rage];

    //NSLog(@"%@",[NSString stringWithFormat:@"%@",[hexDic objectForKey:key]]);

    binaryString = [NSString stringWithFormat:@"%@%@",binaryString,[NSString stringWithFormat:@"%@",[hexDic objectForKey:key]]];

    }

    //NSLog(@"转化后的二进制为:%@",binaryString);

    return binaryString;

    }

    //二进制转十六进制

    - (NSString *)convertDataToHexStr:(NSData *)data {

    if (!data || [data length] == 0) {

    return @"";    }    NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];    [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {        unsigned char *dataBytes = (unsigned char*)bytes;        for (NSInteger i = 0; i < byteRange.length; i++) {            NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];            if ([hexStr length] == 2) {                [string appendString:hexStr];            } else {                [string appendFormat:@"0%@", hexStr];            }        }    }];

    return string;}

    相关文章

      网友评论

          本文标题:进制转换

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