0

Using the Evernote API, I have an object which has an NSUInteger property called hash. For the specific object I'm looking at, this is equal to:

<f5b5444b 33e740b7 f9d49a3b ddb6a39c>

I want to convert this into an NSString. Doing this:

[NSString stringWithFormat:@"%d", noteResource.hash]

Gives me this:

530049088

How could I correctly convert the hash value to an NSString?

Andrew
  • 15,935
  • 28
  • 121
  • 203
  • Eeeeuuuughhhh... Are you using a 128-bit machine? Or how can an `NSInteger` occupy 16 bytes? –  Jul 11 '13 at 11:13
  • Do you want this : http://stackoverflow.com/q/7520615/1603072 ? – Bhavin Jul 11 '13 at 11:15
  • @Vin It looks like it. The remaining problem is that it's an NSUInteger, what I have. How do I convert that to a `const void *`? – Andrew Jul 11 '13 at 11:21
  • 2
    `noteResource` is an `NSData` object, and `noteResource.hash` is some hash value (NSUInteger). What do you mean by *"correctly convert the hash value to an NSString"*? Why convert to `const void *`? What result do you expect? – Martin R Jul 11 '13 at 11:25

1 Answers1

3

When you see something output as "<" 8 hex digits space .... ">", it's the result of logging a NSData object (NSLog(@"%@", myDataObject);). So I believe what you have is not an NSUInteger, but a NSData * object.

There is no built in method to convert between strings and data, you need to do it in code:

- (NSString *)dataToString:(NSData *)data
{
    NSUInteger len = [data length];
    NSMutableString *str = [NSMutableString stringWithCapacity:len*2];
    const uint8_t *bptr = [data bytes];

    while(len--) [str appendFormat:@"%02.2x", *bptr++];

    return str;
}

If this works, you can write your own stringToData method reversing the above, if needed.

David H
  • 40,852
  • 12
  • 92
  • 138