Decimal To Octal


More Conversions



Example Code

NSString *octalString = @"17";

/* Octal to Dec*/
NSLog(@"Decimal Value for Octal String is %ld", strtol(octalString.UTF8String, NULL, 8));

/* Octal to Hex*/
NSLog(@"Hex Value for Octal String is %@",[NSString stringWithFormat:@"%2lX", strtol(octalString.UTF8String, NULL, 8)]);

/* Octal 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 Octal String is %@", binary);

var octalString = "17"

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

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

/* Octal to Binary*/
var binaryString = String(Int(octalString, radix: 8)!, radix: 2)
print("Binary Value for Octal String \(octalString) is ", binaryString);

Result

Decimal Value for Octal String is 15
Hex Value for Octal String is  F
Binary Value for Octal String is 1111

Advertisements