ios 十進制 十六進制 互相轉換

十六進制轉換成十進制spa

1 //16進制轉10進制
2         NSString * temp10 = [NSString stringWithFormat:@"%lu",strtoul([devId UTF8String],0,16)];
3         NSLog(@"心跳數字 10進制 %@",temp10);

code

1 unsigned long long result = 0;
2         NSScanner *scanner = [NSScanner scannerWithString:devId];
3         [scanner scanHexLongLong:&result];
4         NSLog(@"result = %llu",result);

 

十進制轉換成十六進制orm

 1 //將十進制轉化爲十六進制
 2 -(NSString *)ToHex:(long long int)tmpid
 3 {
 4     NSString *nLetterValue;
 5     NSString *str =@"";
 6     long long int ttmpig;
 7     for (int i = 0; i<9; i++) {
 8         ttmpig=tmpid%16;
 9         tmpid=tmpid/16;
10         switch (ttmpig)
11         {
12             case 10:
13                 nLetterValue =@"A";break;
14             case 11:
15                 nLetterValue =@"B";break;
16             case 12:
17                 nLetterValue =@"C";break;
18             case 13:
19                 nLetterValue =@"D";break;
20             case 14:
21                 nLetterValue =@"E";break;
22             case 15:
23                 nLetterValue =@"F";break;
24             default:nLetterValue=[[NSString alloc]initWithFormat:@"%lli",ttmpig];
25                 
26         }
27         str = [nLetterValue stringByAppendingString:str];
28         if (tmpid == 0) {
29             break;
30         }
31         
32     }
33     return str;
34 }
相關文章
相關標籤/搜索