I
I
in_vizz2014-02-09 23:36:01
Objective-C
in_vizz, 2014-02-09 23:36:01

How to deal with my code?

You need to write code to convert from decimal to hexadecimal.
There is a code written in the appcode.
I've been suffering with it for the 3rd day and I can't figure out what's wrong? Help me please.
NSString * dechex(int ​​value)
{
int base = 16;
int initialResult = value;
int initialResultTake = value;
int secondaryOutcome = 0;
NSString *string = @"";
for (int i = 0; initialResult > i; i++)
{
initialResult = initialResult / basa;
secondaryOutcome = initialResultTake % base;
initialResultTake = initialResult;
switch (secondaryOutcome)
{
case 10:
{
string = @"A";
break;
}
case 11:
{
string = @"B";
break;
}
case 12:
{
string = @"C";
break;
}
case 13:
{
string = @"D";
break;
}
case 14:
{
string = @"E";
break;
}
case 15:
{
string = @"F";
break;
}
default:
{
string = [NSString stringWithFormat:@"%i", secondaryOutcome];
break;
}
}
NSLog(@"%@", string);
}
return string;
}
int main(int argc, char *argv[])
{
dechex(12021);
return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rowdy Ro, 2014-02-10
@rowdyro

NSString * dechex(int value)
{
    const NSString* chars = @"0123456789ABCDEF";
    size_t base = chars.length;
    NSString* result = @"";
    while (value >= base) {
        result = [NSString stringWithFormat:@"%@%@",[chars substringWithRange:NSMakeRange(value % base,1)], result];
        value = value / base;
    }
    result = [NSString stringWithFormat:@"%@%@",[chars substringWithRange:NSMakeRange(value,1)], result];
    NSLog(@"%@", result);
    return result;
}

D
Denis Morozov, 2014-03-14
@morozovdenis

NSString * hexFromDex(int a)
{
  return [NSString stringWithFormat:@"%X", a];
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question