7

I need to convert a hex string to binary form in objective-c, Could someone please guide me? For example if i have a hex string 7fefff78, i want to convert it to 1111111111011111111111101111000?

BR, Suppi

Suppi
  • 630
  • 1
  • 7
  • 21

4 Answers4

8

Nice recursive solution...

NSString *hex = @"49cf3e";
NSUInteger hexAsInt;
[[NSScanner scannerWithString:hex] scanHexInt:&hexAsInt];
NSString *binary = [NSString stringWithFormat:@"%@", [self toBinary:hexAsInt]];

-(NSString *)toBinary:(NSUInteger)input
{
    if (input == 1 || input == 0)
        return [NSString stringWithFormat:@"%u", input];
    return [NSString stringWithFormat:@"%@%u", [self toBinary:input / 2], input % 2];
}
Mundi
  • 79,884
  • 17
  • 117
  • 140
2

Simply convert each digit one by one: 0 -> 0000, 7 -> 0111, F -> 1111, etc. A little lookup table could make this very concise.

The beauty of number bases that are powers of another base :-)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Manual conversion? Code 16 cases into a switch-style statement? :) – Jonathan M Aug 25 '11 at 17:24
  • 1
    @Jonathan: A lookup table would suggest itself rather than a switch statement. – Kerrek SB Aug 25 '11 at 17:25
  • There's a programmatic answer at the question this duplicates: http://stackoverflow.com/questions/4618403/convert-binary-to-decimal-in-objective-c . – Jonathan M Aug 25 '11 at 17:26
  • Your solution will work, but hardcoding multiple choices for a math problem is a clumsy solution. There are better ones, more concise and elegant ones available. – Jonathan M Aug 25 '11 at 17:30
  • @Jonathan: Why not post one? :-) We're talking about string to string conversions here, are we? – Kerrek SB Aug 25 '11 at 17:31
  • @Kerreck: I already did by posting that this is a duplicate of a question that has a couple of good solutions already posted. Links provided. – Jonathan M Aug 25 '11 at 17:32
  • 2
    @Jonathan M: That link you posted is the opposite of what the OP asks for. While `strtol` is standard there is no `ltostr` in the standard. – Joe Aug 25 '11 at 17:38
  • @Joe: You're correct. I stand by my objection to this solution, however. It's clumsy. See the recursive solution. – Jonathan M Aug 25 '11 at 21:08
1

In case you need leading zeros, for example 18 returns 00011000 instead of 11000

-(NSString *)toBinary:(NSUInteger)input strLength:(int)length{
        if (input == 1 || input == 0){

         NSString *str=[NSString stringWithFormat:@"%u", input];
            return str;
        }
        else {
            NSString *str=[NSString stringWithFormat:@"%@%u", [self toBinary:input / 2 strLength:0], input % 2];
            if(length>0){
                int reqInt = length * 4;
                for(int i= [str length];i < reqInt;i++){
                    str=[NSString stringWithFormat:@"%@%@",@"0",str];
                }
            }
            return str;
        }  
}
 NSString *hex = @"58";
 NSUInteger hexAsInt;
 [[NSScanner scannerWithString:hex] scanHexInt:&hexAsInt];
 NSString *binary = [NSString stringWithFormat:@"%@", [self toBinary:hexAsInt strLength:[hex length]]];
NSLog(@"binario %@",binary);
Carlos Mayoral
  • 313
  • 3
  • 12
0

I agree with kerrek SB's answer and tried this. Its work for me.

+(NSString *)convertBinaryToHex:(NSString *) strBinary
{
    NSString *strResult = @"";
    NSDictionary *dictBinToHax = [[NSDictionary alloc] initWithObjectsAndKeys:
                                  @"0",@"0000",
                                  @"1",@"0001",
                                  @"2",@"0010",
                                  @"3",@"0011",

                                  @"4",@"0100",
                                  @"5",@"0101",
                                  @"6",@"0110",
                                  @"7",@"0111",

                                  @"8",@"1000",
                                  @"9",@"1001",
                                  @"A",@"1010",
                                  @"B",@"1011",

                                  @"C",@"1100",
                                  @"D",@"1101",
                                  @"E",@"1110",
                                  @"F",@"1111", nil];

    for (int i = 0;i < [strBinary length]; i+=4)
    {
        NSString *strBinaryKey = [strBinary substringWithRange: NSMakeRange(i, 4)];
        strResult = [NSString stringWithFormat:@"%@%@",strResult,[dictBinToHax valueForKey:strBinaryKey]];
    }
    return  strResult;
}
Hardik Darji
  • 3,633
  • 1
  • 30
  • 30