Last active
August 27, 2024 14:50
-
-
Save kumar-aakash86/b3135d96e6e9c9b01516501c7d0058b5 to your computer and use it in GitHub Desktop.
Smiley code to unicode converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Full imoji list - https://unicode.org/emoji/charts/full-emoji-list.html | |
String encodeToUtf16(String unicodeString) { | |
int rune = unicodeString.runes.first; | |
if (rune >= 0x10000) { | |
int high = (rune - 0x10000) ~/ 0x400 + 0xD800; | |
int low = (rune - 0x10000) % 0x400 + 0xDC00; | |
return String.fromCharCode(high) + String.fromCharCode(low); | |
} else { | |
return unicodeString; | |
} | |
} | |
String utf16ToEscapedString(List<int> codeUnits) { | |
return codeUnits.map((unit) { | |
return "\\u${unit.toRadixString(16).padLeft(4, '0').toUpperCase()}"; | |
}).join(); | |
} | |
void main() { | |
String unicodeString = '\u{2757}'; | |
String utf16Encoded = encodeToUtf16(unicodeString); | |
print(utf16Encoded); // 😊 | |
print(utf16ToEscapedString(utf16Encoded.codeUnits)); // [55357, 56842] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment