I'm trying to convert emojis in hex values, I found some code online to do it but it's only working using Objective C, how to do the same with Swift?
Asked
Active
Viewed 7,147 times
3 Answers
11
This is a "pure Swift" method, without using Foundation:
let smiley = ""
let uni = smiley.unicodeScalars // Unicode scalar values of the string
let unicode = uni[uni.startIndex].value // First element as an UInt32
print(String(unicode, radix: 16, uppercase: true))
// Output: 1F60A
Note that a Swift Character
represents a "Unicode grapheme cluster"
(compare Strings in Swift 2 from the Swift blog) which can
consist of several "Unicode scalar values". Taking the example
from @TomSawyer's comment below:
let zero = "0️⃣"
let uni = zero.unicodeScalars // Unicode scalar values of the string
let unicodes = uni.map { $0.value }
print(unicodes.map { String($0, radix: 16, uppercase: true) } )
// Output: ["30", "FE0F", "20E3"]

Martin R
- 529,903
- 94
- 1,240
- 1,382
-
doesn't work with combination emoji like 0️⃣ , ❤️ – TomSawyer Dec 11 '15 at 20:05
-
@TomSawyer: `0️⃣` consists of three Unicode code points: U+0030 (the character "0"), followed by U+FE0F (VARIATION SELECTOR-16) and U+20E3 (COMBINING ENCLOSING KEYCAP) – What do you think the correct output should be? – Martin R Dec 11 '15 at 20:16
-
1@TomSawyer: `❤️` is not even a single character, it is the *String* composed of the characters `"", "❤️", "", ""`. – Martin R Dec 11 '15 at 20:44
-
@TomSawyer: Some feedback how it "doesn't work" and what the correct output should be (in your opinion) would be much appreciated ... – Martin R May 30 '17 at 15:32
-
@MartinR Any way to get the name of emoji like- :smile:, :angry: and so on ? – iDeveloper Aug 19 '21 at 08:02
-
@iDeveloper: This might be what you are looking for: https://stackoverflow.com/q/24699576/1187415. – Martin R Aug 19 '21 at 10:31
-
Hey @MartinR I go through this before, But It's giving me full description of emoji, Instead of just name. What i am looking for just name of selected emoji. Example- If I selected So I want the name emoji e.g. blush – iDeveloper Aug 19 '21 at 11:23
-
@iDeveloper: I am sorry but I cannot help you with that. I do not know if such a method exists. – Martin R Aug 19 '21 at 14:46
6
If some one trying to found a way to convert Emoji To Unicode string
extension String {
func decode() -> String {
let data = self.data(using: .utf8)!
return String(data: data, encoding: .nonLossyASCII) ?? self
}
func encode() -> String {
let data = self.data(using: .nonLossyASCII, allowLossyConversion: true)!
return String(data: data, encoding: .utf8)!
}
}
Example:
- "".encode()
RESULT: \ud83d\ude0d
- "\ud83d\ude0d".decode()
RESULT:

Gokul G
- 2,046
- 15
- 22
-
this works fine, but there is a problem with characters in other languages: For example: "Ä" -- this letter is not encoding properly with the above way – Suhas Arvind Patil Feb 05 '20 at 13:11
1
It works similarly but pay attention when you're printing it:
import Foundation
var smiley = ""
var data: NSData = smiley.dataUsingEncoding(NSUTF32LittleEndianStringEncoding, allowLossyConversion: false)!
var unicode:UInt32 = UInt32()
data.getBytes(&unicode)
// println(unicode) // Prints the decimal value
println(NSString(format:"%2X", unicode)) // Print the hex value of the smiley

Armand Grillet
- 3,229
- 5
- 30
- 60