Reputation: 1138
I have an iOS app and PHP web service that take a user submitted "password" and hash it.
The problem arises when you enter an emoji:
PHP hashes the salt + 😍
iOS hashes the salt + [heart-for-eyes smiley emoji]
Is there a way to convert the emoji on iOS to the corresponding HTML Entity (like PHP does automatically) so that the resulting hash values are the same?
stringWithUTF8String
does not workNSNonLossyASCIIStringEncoding
converts it to "\ud83d\ude0d"Upvotes: 0
Views: 1788
Reputation: 112855
Here is a solution, perhaps there is a built-in:
NSString *emojiString = @"😍";
NSLog(@"emojiString: %@", emojiString);
NSData *codePointData = [emojiString dataUsingEncoding:NSUTF32LittleEndianStringEncoding];
u_int32_t codePoint = *((u_int32_t *)(codePointData.bytes));
NSString *escapedEmoji = [NSString stringWithFormat:@"&#%d", codePoint];
NSLog(@"escapedEmoji: %@", escapedEmoji);
NSLog output:
emojiString: 😍
escapedEmoji: 😍
This based on the fact that the escaped emoji is the decimal number of the code point. The binary code point is the UTF-32 encoding of the unicode character.
Unfortunately I was not able to find a pre-existing solution.
Upvotes: 1