2016-05-09 22:48:49 l2i2j2 阅读数 602
- (void)RGBWithHexColor:(int)hexColor {
    int red = ((hexColor & 0xFF0000) >> 16);
    int green = ((hexColor & 0xFF00) >> 8);
    int blue = (hexColor & 0xFF);
    NSLog(@"red:%d  green:%d  blue:%d", red, green, blue);
}

2016-04-05 15:43:39 qq_26705543 阅读数 738

一、整形装换成两个字节

    

    int value = 456;

    unsigned char  byte1 = (value & 0xff00)>>8//8

    unsigned char  byte2 = (value & 0xff);//8

    

    NSLog(@"byte1= %x   byte2= %x ",byte1,byte2);

  

        byte1 = 1;  byte2= c8;

二、两个字节转换成十进制整数

    char bytes[]={byte1,byte2};

    unsigned char  by1 = (bytes[0] & 0xff); //8

    unsigned char  by2 = (bytes[1] & 0xff);//8

    int   temp  = (by2|(by1<<8));

    NSLog(@"temp = %d",temp);

    

    temp = 456;

三、整形装换成三个字节

    int value =134456;

    unsigned char  byte1 = (value & 0xff0000)>>16;//最高8

    unsigned char  byte2 = (value & 0xff00)>>8;//中间8

    unsigned char  byte3 = (value & 0xff);//8

    

    NSLog(@"byte1= %x   byte2= %x byte3= %x ",byte1,byte2,byte3);

    byte1= 2   byte2= d byte3= 38


四、三个字节转换成十进制整数

    char bytes[]={byte1,byte2,byte3};

    unsigned char  by1 = (bytes[0] & 0xff); //8

    unsigned char  by2 = (bytes[1] & 0xff);//8

    unsigned char  by3 = (bytes[2] & 0xff);//8

    

    int   temp  = (by3|(by2<<8)|(by1<<16));

    NSLog(@"temp = %d",temp);

   temp = 134456

如果还有更多的自己接需要转换以此类推

2015-08-01 12:00:19 sinat_25449693 阅读数 1558

2to10

NSString *base = @"0110011011001100";
NSLog(@"%@",base);
int i = 0,j = 0;
for (int k = 0; k<base.length; k++) {
    j = [[base substringWithRange:NSMakeRange(k, 1)] intValue];
    j= j *powf(2, base.length-k-1);
    i += j;
}
NSString *base10 = [NSString stringWithFormat:@"%d",i];
NSLog(@"%@",base10);

10to16

NSString *base16 = [[NSString alloc]initWithFormat:@"%x",[base10 intValue]];
NSLog(@"%@",base16);

16to2

    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 *str = [[NSString alloc]init];
    for (int i = 0; i<[base16 length]; i++) {
        NSRange rage;
        rage.length = 1;
        rage.location = i;
        NSString *key = [base16 substringWithRange:rage];
       str = [NSString stringWithFormat:@"%@%@",str,[NSString stringWithFormat:@"%@",[hexDic objectForKey:key]]];
    }
NSLog(@"%@",str);
2016-07-05 10:49:44 wujianjun6932 阅读数 1651

    //10进制转16进制字符串

    NSString *hexString= [NSString stringWithFormat:@"%@",[[NSString alloc] initWithFormat:@"%1lx",20]];

    

2017-04-05 11:41:37 Iven_ma 阅读数 255
//tmpid 要转化的进制数  
//length 要转化的进制长度
- (void)systemTmpid:(NSInteger)tmpid withLength:(int)length {

    NSString *a = @"";
    while (tmpid)
    {
        a = [[NSString stringWithFormat:@"%ld",tmpid%2] stringByAppendingString:a];
        if (tmpid/2 < 1)
        {
            break;
        }
        tmpid = tmpid/2 ;
    }

    if (a.length <= length)
    {
        NSMutableString *b = [[NSMutableString alloc]init];;
        for (int i = 0; i < length - a.length; i++)
        {
            [b appendString:@"0"];
        }

        a = [b stringByAppendingString:a];
    }

    NSLog(@"---%@",a);

}

iOS中16进制转10进制

阅读数 1166

ios进制转换

阅读数 418

iOS 16进制转成汉字

阅读数 624

没有更多推荐了,返回首页