Advertisements
Decimal to Hexadecimal Convertor
Example Code
NSString *decimalString = @"15";
/* Decimal to Hex*/
NSString *hex = [NSString stringWithFormat:@"%2lX", (unsigned long)[decimalString integerValue]];
NSLog(@"Hexadecimal Value for Decimal String is %@", hex);
/* Decimal to Octal*/
NSString *octal = [NSString stringWithFormat:@"%2lO", (unsigned long)[decimalString integerValue]];
NSLog(@"Octal Value for Decimal String is %@", octal);
/* Decimal to Binary*/
NSUInteger decimalNumber = [decimalString integerValue];
int index = 0;
NSString *binary = @"";
while (decimalNumber > 0) {
binary = [[NSString stringWithFormat:@"%lu", decimalNumber&1] stringByAppendingString:binary];
decimalNumber = decimalNumber>> 1;
++index;
}
NSLog(@"Binary Value for Decimal String is %@", binary);
Result
Hexadecimal Value for Decimal String is F Octal Value for Decimal String is 17 Binary Value for Decimal String is 1111
Advertisements