十进制转二进制
/**
转化为十进制
@param binary 二进制的数据
@return 数据结果
*/
- (NSString *)convertDecimalSystemFromBinarySystem:(NSString *)binary
{
NSInteger ll = 0 ;
NSInteger temp = 0 ;
for (NSInteger i = 0; i < binary.length; i ++){
temp = [[binary substringWithRange:NSMakeRange(i, 1)] intValue];
temp = temp * powf(2, binary.length - i - 1);
ll += temp;
}
NSString * result = [NSString stringWithFormat:@"%ld",ll];
return result;
}
十进制转二进制
/**
转化为二进制
@param decimal 十进制的数据
@return 二进制的结果
*/
- (NSString *)getBinaryByDecimal:(NSInteger)num {
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;
}
十进制转十六进制
/**
十进制转十六进制
@param tmpid 数据
@return 结果
*/
-(NSString *)ToHex:(long long int)tmpid
{
NSString *nLetterValue;
NSString *str =@"";
long long int ttmpig;
for (int i = 0; i<9; i++) {
ttmpig=tmpid%16;
tmpid=tmpid/16;
switch (ttmpig)
{
case 10:
nLetterValue =@"A";break;
case 11:
nLetterValue =@"B";break;
case 12:
nLetterValue =@"C";break;
case 13:
nLetterValue =@"D";break;
case 14:
nLetterValue =@"E";break;
case 15:
nLetterValue =@"F";break;
default:nLetterValue=[[NSString alloc]initWithFormat:@"%lli",ttmpig];
}
str = [nLetterValue stringByAppendingString:str];
if (tmpid == 0) {
break;
}
}
return str;
}