views:

344

answers:

1

Does anyone have a code snippet or a class that will take a long long and turn it into a 16 byte Hex string?

I'm looking to turn data like this

long long decimalRepresentation = 1719886131591410351;

and turn it into this

//Base 16 Hex Output: 17DE435307A07300

The %x operator doesn't want to work for me

NSLog(@"Hex: %x",decimalRepresentation);
//console : "Hex: 7a072af"

As you can see that's not even close. Any help is truly appreciated!

+2  A: 

%x prints an unsigned integer in hexadecimal representation and sizeof(long long) != sizeof(unsigned). See e.g. "Data Type Size and Alignment" in the 64bit transitioning guide.

Use the ll specifier (thats two lower-case L) to get the desired output:

NSLog(@"%llx", myLongLong); 
Georg Fritzsche
Thank you, that was quick! I haven't tried this on iPhone yet, do you know if it works on the iPhone processor?
JustinXXVII
The `printf()` format is well specified, so i wouldn't expect any surprises there.
Georg Fritzsche
nevmind my bad sorry
JustinXXVII
You should clarify in your answer that the %llx is actually two lowercase Ls, and not two number ones. I had to copy and paste your answer to realize this.
JustinXXVII
@Justin: Oh, ok - i guess for me *"`long` -> lowercase L"* is hardwired now.
Georg Fritzsche