ios 十进制 十六进制 互相转换
十六进制转换成十进制
1 //16进制转10进制 2 NSString * temp10 = [NSString stringWithFormat:@"%lu",strtoul([devId UTF8String],0,16)]; 3 NSLog(@"心跳数字 10进制 %@",temp10);
或
1 unsigned long long result = 0; 2 NSScanner *scanner = [NSScanner scannerWithString:devId]; 3 [scanner scanHexLongLong:&result]; 4 NSLog(@"result = %llu",result);
十进制转换成十六进制
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 }
posted on 2016-11-02 10:02 codemaker313 阅读(6498) 评论(0) 编辑 收藏 举报