Andrew
Andrew

Reputation: 16051

Convert a hash to NSString?

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?

Upvotes: 0

Views: 577

Answers (1)

David Hoerl
David Hoerl

Reputation: 41652

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.

Upvotes: 3

Related Questions