Binary To Decimal


More Conversions



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);
var decimalString = "15"

/* Decimal to Hex*/
var hexString = String(Int(decimalString, radix: 10)!, radix: 16)
print("Hexadecimal Value for Decimal String \(decimalString) is ", hexString);

/* Decimal to Octal*/
var octalString = String(Int(decimalString, radix: 10)!, radix: 8)
print("Octal Value for Decimal String \(decimalString) is ", octalString);

/* Decimal to Binary*/
var binaryString = String(Int(decimalString, radix: 10)!, radix: 2)
print("Binary Value for Decimal String \(decimalString) is ", binaryString);


Result

Hexadecimal Value for Decimal String is  F
Octal Value for Decimal String is 17
Binary Value for Decimal String is 1111

Advertisements