NaveenKumar NaveenKumar - 3 months ago 27x
iOS Question

Hexadecimal or decimal to binary in Objective C

I was searching for methods in Objective C to convert hexadecimal to decimal or binary.

But,I am unable to find it so far

Any ideas are welcome


Following are methods to convert into binary and hexadecimal ( If you are expecting string from integer value.)

1) To convert into binary

- (NSString *)binaryStringWithInteger:(NSInteger)value
    NSMutableString *string = [NSMutableString string];
    while (value)
    [string insertString:(value & 1)? @"1": @"0" atIndex:0];
    value /= 2;
   return string;

2) To convert into hexadecimal

Hexadecimal is just a way of displaying an integer.

if you want to create a string that is the hexadecimal representation of an integer, you can use this code:

- (NSString *)hexFromInt:(NSInteger)val
return [NSString stringWithFormat:@"0x%X", val];