NaveenKumar NaveenKumar - 2 years ago 161
iOS Question

Hexadecimal or decimal to binary

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

So far I am unable to find it.

Any ideas would be helpful !

Answer Source

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];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download