Take the 2-minute tour ×

I need to convert values like 1393443048683555715 to HEX. But, first of all, i cann't display it as decimal using NSLog(), for example.

Ok, it works:

NSLog(@"%qu", 1393443048683555706);

But what about converting to HEX. What type i have to use to store this big value?

NSLog([NSString stringWithFormat: @"%x", 1393443048683555706]);
// result eb854b7a. It's incorrect result!

but i forgot to say that this big number represented as string @"1393443048683555706" (not int)

share|improve this question
I've had some luck using one of the various arbitrary precision integer libraries for C. –  jessecurry Feb 10 '12 at 17:10
why not convert it to a string? –  James Black Feb 10 '12 at 17:12
Since all numbers are in binary form, I guess you need a string representation of a number in hex, right? What about [NSString stringWithFormat: @"%x", someInteger]? –  Costique Feb 10 '12 at 17:45
it gives incorrect result –  leaf Feb 10 '12 at 17:50
You could always convert the integer value to Hex yourself. My guess you have a syntax problem dealing with NSLOG –  Ramhound Feb 10 '12 at 17:54
add comment

2 Answers

up vote 1 down vote accepted

The "x" format specifier is for 32-bit numbers; you need to use either "qx" or "qX" (depending on whether you want the letter values to be uppercase or not). These are the formatters for unsigned long long values, see:


Next, you should not pass a string as you have done above directly to NSLog - this can cause a crash.

NSLog(string); // bad!!
NSLog(@"%@", string); // good

So if your value comes as a string, you'll want to do this:

NSString *longNumber = @"1393443048683555706";
NSLog(@"%qx", [longNumber longLongValue]);

If the string value can't be coerced to a number, longLongValue will return 0. I'll leave it to you do handle the error (and bounds) checking - see NSString for details.

If you want to save the hex value as a string, do this:

NSString *hexRepresentation = [NSString stringWithFormat:@"%qx", [longNumber longLongValue]];

Again, best to take care for error handling.

share|improve this answer
thanq. perfect answer :) –  leaf Feb 10 '12 at 19:53
add comment

You can use %qi and %qu format specifiers with NSLog to display 64-bit integers. Your constant appears to fit in 64-bit signed number, with the limits of:

[−9223372036854775808 to 9223372036854775807]
share|improve this answer
add comment

Your Answer


