最近要搞跟单片机通讯要很多进制转换的。留作纪念
二进制转10进制NSSing *strZone=@"1000111000111";
int zoneHour=0;
//2进制转10进制
for (int i=strZone.length-1; i>=0; i--)
{
NSString *a=[strZone substringWithRange:NSMakeRange(i,1)];
zoneHour+=[a intValue]*pow(2, strZone.length-1-i);
NSLog(@"a=%@ zoneHour=%d dd=%d hh=%1f",a,zoneHour,strZone.length-1-i,pow(2, strZone.length-1-i));
}
10进制转2进制
-(NSString *)toBinary:(NSInteger)input
{
if (input == 1 || input == 0) {
return [NSString stringWithFormat:@"%d", input];
}
else {
return [NSString stringWithFormat:@"%@%d", [self toBinary:input / 2], input % 2];
}
}
10进制转16进制
int b= 122;
NSData* data = [NSData dataWithBytes:&a length:1];
[timeData appendData:data];
int a = 20;
NSString* str = [NSString stringWithFormat:@"0x%x", a];
NSData* data = [NSData dataWithBytes:[str UTF8String] length:strlen([str UTF8String])];
16 进制转2进制
NSString *str = @"Af01";
NSMutableString *binStr = [[NSMutableString alloc] init];
for(NSUInteger i=0; i<[str length]; i++)
{
[binStr appendString:[self hexToBinary:[str characterAtIndex:i]]];
}
NSLog(@"Bin: %@", binStr);
- (NSString *) hexToBinary:(unichar)myChar
{
switch(myChar)
{
case '0': return @"0000";
case '1': return @"0001";
case '2': return @"0010";
case '3': return @"0011";
case '4': return @"0100";
case '5': return @"0101";
case '6': return @"0110";
case '7': return @"0111";
case '8': return @"1000";
case '9': return @"1001";
case 'a':
case 'A': return @"1010";
case 'b':
case 'B': return @"1011";
case 'c':
case 'C': return @"1100";
case 'd':
case 'D': return @"1101";
case 'e':
case 'E': return @"1110";
case 'f':
case 'F': return @"1111";
}
return @"-1"; //means something went wrong, shouldn't reach here!
}
二进制转10进制NSSing *strZone=@"1000111000111";
int zoneHour=0;
//2进制转10进制
for (int i=strZone.length-1; i>=0; i--)
{
NSString *a=[strZone substringWithRange:NSMakeRange(i,1)];
zoneHour+=[a intValue]*pow(2, strZone.length-1-i);
NSLog(@"a=%@ zoneHour=%d dd=%d hh=%1f",a,zoneHour,strZone.length-1-i,pow(2, strZone.length-1-i));
}
10进制转2进制
-(NSString *)toBinary:(NSInteger)input
{
if (input == 1 || input == 0) {
return [NSString stringWithFormat:@"%d", input];
}
else {
return [NSString stringWithFormat:@"%@%d", [self toBinary:input / 2], input % 2];
}
}
10进制转16进制
int b= 122;
NSData* data = [NSData dataWithBytes:&a length:1];
[timeData appendData:data];
int a = 20;
NSString* str = [NSString stringWithFormat:@"0x%x", a];
NSData* data = [NSData dataWithBytes:[str UTF8String] length:strlen([str UTF8String])];
16 进制转2进制
NSString *str = @"Af01";
NSMutableString *binStr = [[NSMutableString alloc] init];
for(NSUInteger i=0; i<[str length]; i++)
{
[binStr appendString:[self hexToBinary:[str characterAtIndex:i]]];
}
NSLog(@"Bin: %@", binStr);
- (NSString *) hexToBinary:(unichar)myChar
{
switch(myChar)
{
case '0': return @"0000";
case '1': return @"0001";
case '2': return @"0010";
case '3': return @"0011";
case '4': return @"0100";
case '5': return @"0101";
case '6': return @"0110";
case '7': return @"0111";
case '8': return @"1000";
case '9': return @"1001";
case 'a':
case 'A': return @"1010";
case 'b':
case 'B': return @"1011";
case 'c':
case 'C': return @"1100";
case 'd':
case 'D': return @"1101";
case 'e':
case 'E': return @"1110";
case 'f':
case 'F': return @"1111";
}
return @"-1"; //means something went wrong, shouldn't reach here!
}