Matisse VerDuyn
Matisse VerDuyn

Reputation: 1138

NSString Convert Emojis to HTML Entities

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:

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?

Upvotes: 0

Views: 1788

Answers (1)

zaph
zaph

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: &#128525

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

Related Questions