Reputation: 439
I've got a problem where the MD5 string that my method returns has all uppercase letters. Is there a way other than using [string lowerCaseString]
to make the output of the method return a lowercase string? The above method seems hackish to me.
Here is the method that I'm using:
- (NSString *)MD5String {
const char *cstr = [self UTF8String];
unsigned char result[16];
CC_MD5(cstr, strlen(cstr), result);
return [NSString stringWithFormat:
@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
}
Upvotes: 1
Views: 964
Reputation: 3591
Why hackish? The case distinction in an MD5 result makes no sense because in truth it's not a string it is returning but a couple of hexadecimal digits. The proper amend would be to make your comparison method (assuming you're comparing hashes later) ignore the case of it, as it should not matter.
In any case, you can make it lowercase by changing the format specifiers to lowercase:
return [NSString stringWithFormat:
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
Upvotes: 4