//2进制转10进制
- (NSString *)toDecimalSystemWithBinarySystem:(NSString *)binary
{
int ll = 0 ;
int temp = 0 ;
for (int 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:@"%d",ll];
return result;
}
字符串转换16进制
NSString *hexString = [NSString stringWithFormat:@"%@",[[NSString alloc] initWithFormat:@"%1x",number]];
NSString转char
char mychar[string.length];
strcpy(mychar,(char *)[string UTF8String]);
char ch = mychar[i];
原文:http://www.cocoachina.com/bbs/read.php?tid=141348
/将十进制转化为二进制,设置返回NSString 长度
+ (NSString *)decimalTOBinary:(uint16_t)tmpid backLength:(int)length
{
NSString *a = @"";
while (tmpid)
{
a = [[NSString stringWithFormat:@"%d",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];
[b release];
}
return a;
}
//将十进制转化为十六进制
- (NSString *)ToHex:(uint16_t)tmpid
{
NSString *nLetterValue;
NSString *str =@"";
uint16_t 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 stringWithFormat:@"%u",ttmpig];
}
str = [nLetterValue stringByAppendingString:str];
if (tmpid == 0) {
break;
}
}
return str;
}
//将16进制转化为二进制
-(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;
}
进制转换
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...