Reputation:
I need to convert a long value from int64 to NSData, so I can later run a hash algorithm on it. I perform:
int64_t longNumber = 9000000000000000000L;
NSMutableData *buffer = [NSMutableData dataWithBytes:&longNumber length:sizeof(longNumber)];
NSLog(@"%lld", [buffer bytes]);
NSLog(@"%lld", longNumber);
The resultant console output is like this:
6201314301187184 9000000000000000000
Why is NSData not properly storing the value of the long number? If I run this in a loop, the NSData bytes drift, starting with 620, then 621 and on. Am I outputting the address of the longNumber via [buffer bytes] and not its value?
Upvotes: 3
Views: 3553
Reputation: 78343
You have two major issues: first, your number is too large for the long that you are casting it to. Instead of 9000000000000000000L
you should have 9000000000000000000LL
to indicate a long long constant.
Second, you answered your question correctly, you are printing out an address. Replace your NSLog line with with this line:
NSLog(@"%lld", *((int64_t*)[buffer bytes]));
and you should see the result you expect.
Upvotes: 7