views:

3631

answers:

2

Given the example of an array like this:

idx = [ 0xe, 0x3,  0x6, 0x8, 0x2 ]

I want to get an integer and string representation of each of the specified items in Objective C. I have mocked up a ruby example which works perfectly:

0xe gives 14 when i run 0xe.to_i and "e" when i run to_i(base=16)
0x3 gives 3 when i run 0x3.to_i and gives 3 when i run to_i(base=16)

How can I achieve this in Objective C?

+1  A: 

To get the decimal and hexadecimal equivalents, you would do:

int number = 0xe; // or 0x3, 0x6, 0x8, 0x2

NSString * decimalString = [NSString stringWithFormat:@"%d", number];
NSString * hexString = [NSString stringWithFormat:@"%x", number];
e.James
Thanks, Ok that's a bit helpfull. But can I store 0xe in NSNumber? I cannot assign it to a int as i want to store this stuff in NSArray as you can't use standard C types in it (mutable or not)
Grzegorz Kazulak
NSNumber can wrap any of the primitive numeric types. Of course, once you're making string representations of the numbers, you could store the strings in the array.
Peter Hosey
A: 

Once you have an NSString you can just use the methods in that class, like intValue or longValue or floatValue

gravisman