NaveenKumar NaveenKumar - 2 months ago 34x
iOS Question

How to convert Hexadecimal or Decimal to Binary in Objective C

As far I searched there is no API to convert hexadecimal to decimal or binary

Is there any workarounds ?


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];