Skip to content

Instantly share code, notes, and snippets.

@lcb
Last active January 31, 2023 17:22
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lcb/4a34e2779ede7c027c37d5f48ec313f6 to your computer and use it in GitHub Desktop.
Save lcb/4a34e2779ede7c027c37d5f48ec313f6 to your computer and use it in GitHub Desktop.
emojIDs

EmojID

The function emojid() deterministically converts hexadecimal strings (like sha1sum, md5sum, uuid4, uuid5) into emoji containing strings, remojid() converts back.

Implementations in Javascript and Python are provided.

What for?

¯\_(ツ)_/¯

How it works

The code will in each step

  • take the value of 2 hex chars
  • add a "magic" offset (U+1F400) to it
  • convert it to unicode

There is a bit of tolerance built in: Non hex chars will be ignored and simply be added to the result. But I wouldn't push it too hard.

Examples

md5
emojid(42eab827e162899feb01af513c39be46) =>👂📪💸🐧📡👢💉💟📫🐁💯👑🐼🐹💾👆

shasum
emojid(d50aabf4b2f62569b976a4b16d6dfc86750499a7) => 📕🐊💫📴💲📶🐥👩💹👶💤💱👭👭📼💆👵🐄💙💧

git short hash
emojid(21132ad) => 🐡🐓🐪d 

uuid4
emojid(f6e3299a-525a-4c5f-bebc-ef014b608a66) => 📶📣🐩💚-👒👚-👌👟-💾💼-📯🐁👋👠💊👦

reverse
remojid(🐡🐓🐪d) => 21132ad
function emojid(id) {
let word = '';
let res = '';
id.toLowerCase().split('').map(c => {
word += c;
if(c.search(/[0-9a-f]/) === -1) {
//flush
res += word;
word= '';
return;
}
if (word.length === 2) {
res += String.fromCodePoint(0x1F400 + parseInt(word, 16));
word = '';
}
});
return res + word;
}
function remojid(emojid) {
return [...emojid].map(c => {
const intVal = c.codePointAt(0) - 0x1F400;
const hexVal = intVal.toString(16);
const pad = hexVal.length < 2 ? '0' : '';
return intVal > 0 ? pad + hexVal : c;
}).join('');
}
console.log(emojid('6777576b-3216-444e-960b-20a07bd6c352'));
console.log(remojid(emojid('6777576b-3216-444e-960b-20a07bd6c352')));
import re
def emojid(id):
word = ''
res = ''
for c in list(id.lower()):
word += c
if re.search("[0-9a-f]", c) == None:
#flush
res += word
word = ''
continue
if (len(word) == 2):
ucode = 0x1F400 + int(word, 16)
res += unicode('\U000%x' % ucode , 'unicode-escape')
word = ''
return res + word
def remojid(emojid):
res = ''
for c in emojid:
intVal = ord(c) - 56320
if intVal == -963: continue
if intVal < 0:
res += c
continue
res += "%02x" % intVal
return res
print emojid('6777576b-3216-444e-960b-20a07bd6c352')
print remojid(emojid('6777576b-3216-444e-960b-20a07bd6c352'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment