Cindy Broutin
Cindy Broutin

Reputation: 197

Decimal to Hex - Cocoa/Objective-C

I need to convert some strings which have decimal content. I want these contents in a string, in hex format...

How can I do it?

I tried to use NSScanner but this method seems to be a little bit huge to just make a dec-hex calculation?

Thanks for your answers!

Upvotes: 2

Views: 8985

Answers (1)

Joe
Joe

Reputation: 57169

If you know your string only contains a valid decimal number then the simplest way would be:

NSString *dec = @"254";
NSString *hex = [NSString stringWithFormat:@"0x%lX", 
                  (unsigned long)[dec integerValue]];
NSLog(@"%@", hex);

Upvotes: 18

Related Questions