2

I want to encode this emoji 😈 to string:

I have tried like this:

let data = self.comment.dataUsingEncoding(NSNonLossyASCIIStringEncoding, allowLossyConversion: true) if let data = data { let emojiString = NSString(data: data, encoding: NSUTF8StringEncoding) as! String self.comment = emojiString } 

but it doesn't work.

enter image description here

I want to encode like 😈 , not \ud83d\ude08

So how to do this?

1 Answer 1

4

128520 is the unicode scalar value of "😈":

let text = "😈😀" let encoded = text.unicodeScalars.map { "&#" + String($0.value) + ";" }.joinWithSeparator("") print(encoded) // 😈😀 
Sign up to request clarification or add additional context in comments.

3 Comments

This helps me. Thanks a lot @Martin R 😈.
it should be let unicode = text.unicodeScalars.map({ "&#" + String($0.value) }).joinWithSeparator(",") to get expected string :D
@Hoa: OP said: "I want to encode like 😈" and that is the syntax of XML "character entities". But anyway, it should be easy to adapt the example code to the concrete needs.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.