Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Creating Flags with Unicode and ISO Codes

Hello everyone,

Today, I would like to share a neat trick that I've recently used in one of my projects. As you may or may not know, it's possible to use Unicode to combine various emojis together to create a new one. For example, taking the 'person' emoji and adding a 'skin color' emoji can give us a person that corresponds to the correct skin color.

Interestingly, this can also be applied to emojis that represent regional letters. For example, if I take the letter 'F' and then the letter 'R', it automatically gives the French flag. This works with all ISO codes for different countries. So, if you need to display a flag in an application, rather than searching for PNGs or an icon library corresponding to the different flags, you can use this trick.

This is also faster to load for the user.

Start by taking the ISO code and split it into individual letters. Convert the string into an array of letters.

Code
.split('')

Convert these letters into their ASCII position.

Code
.map(letter => letter.charCodeAt(0));

After getting the position, use the remainder of division by 32. The goal is to start the alphabet from 1 (i.e., 'A' or 'a' is 1, 'B' or 'b' is 2, and so on). Doing modulo 32 allows us to get 1 for lowercase and uppercase 'a', which is quite convenient. Also take the position of the regional letter 'a', subtract 1, and add this number to what we got previously. This converts the lowercase or uppercase 'a' into the Unicode 'a' version.
https://www.isthisthingon.org/unicode/in...lyph=1F1E6

Code
.map(letter => (letter % 32) + 0x1F1E5);

Now that we have these numbers, we can convert them back into letters using the 'fromCodePoint' method.

Code
.map(code => String.fromCodePoint(code));

Finally, we can use the 'join' function to recreate a string composed of these two letters. As if by magic, the French flag appears!

Code
.join('');
console.log(flag);  // 🇫🇷

I hope you found this trick useful!

Code
function isoToFlag(code) {
    return code
        .split('')
        .map(letter => (letter.charCodeAt(0) % 32) + 0x1F1E5)
        .map(n => String.fromCodePoint(n))
        .join('')
}

console.log(isoToFlag("FR"));  // Outputs: 🇫🇷
console.log(isoToFlag("BE"));  // Outputs: 🇧🇪
console.log(isoToFlag("CA"));  // Outputs: 🇨🇦
console.log(isoToFlag("US"));  // Outputs: 🇺🇸
[Image: vibesignature.gif]


“Whatever Your Mind Can Conceive and Believe, It Can Achieve.” – Napoleon Hill
NQ my boy!
You definitely in my new group

And great thread... HQAF
[Image: vibesignature.gif]
The Brotherhood Vibe  Legends
(Jul 14, 2023 08:27 AM)7 Wrote:  NQ my boy!
You definitely in my new group

And great thread... HQAF

Thanks my man 💯
[Image: vibesignature.gif]


“Whatever Your Mind Can Conceive and Believe, It Can Achieve.” – Napoleon Hill
Newquery is the best coder on HF way better then dashpump
(Jul 14, 2023 09:37 AM)wtf Wrote:  Newquery is the best coder on HF way better then dashpump

Yooo no cap! This little trick is actually so cool! Thanks for sharing! Haha
[Image: O444cFs.jpg]
Is this dependent on certain devices? For example on mine, I get a print out of 'FR BE CA US' and I can tell the font is a little different, but it's not showing flag emojis. It might require the user or system/app to have the emojis available already.

Either way I'll take a deeper look later. thanks!
(Jul 14, 2023 03:03 PM)Hackers Wrote:  Is this dependent on certain devices? For example on mine, I get a print out of 'FR BE CA US' and I can tell the font is a little different, but it's not showing flag emojis. It might require the user or system/app to have the emojis available already.

Either way I'll take a deeper look later. thanks!

emojis are controlled by your font, since they are just unicode characters. hence why theres always problems on linux if you try to use chinese/japanese/korean programs/files without installing whatever *_cjk font package.

ideally, most system fonts have extremely basic emoji support (e.g., you are seeing "FR" for france flag, and im guessing if you tried to render a smileyface emoji, you would get a smileyface character like the windows ALT+1 smiley.) but ofc its better to just install a font that will support certain codepoint ranges, like fonts for CJK and emoji.

ex.
my bash shell is disgusting and will just print "???????" for cjk filenames pretending that japanese is not real and only english ascii is supported ootb, like we are in fkn 1970
at least zsh will print the escape sequences and let me tab-complete it.
humble rubyist in a world of pythonistas
(Jul 14, 2023 04:24 PM)Faith Connors Wrote:  emojis are controlled by your font, since they are just unicode characters. hence why theres always problems on linux if you try to use chinese/japanese/korean programs/files without installing whatever *_cjk font package.

True, and I'm surprised Chrome doesn't have a built in font pack for flags! I'll have to get something installed to support it, but this would be based on the font family of the web page or app using the flags I think so even then it might not work

The unicode trick is cool though, I'll see how we can use this at work to enhance some of the locale/translate features we add to client sites.
(Jul 14, 2023 09:37 AM)wtf Wrote:  Newquery is the best coder on HF way better then dashpump

Thank you hackforums tbh

(Jul 14, 2023 11:04 AM)DashPHP Wrote:  Yooo no cap! This little trick is actually so cool! Thanks for sharing! Haha

😎 Glad you like it !
[Image: vibesignature.gif]


“Whatever Your Mind Can Conceive and Believe, It Can Achieve.” – Napoleon Hill


Possibly Related Threads...
Thread / Author Replies Views Last Post
[PHP] by PDGLOBALMar 28, 2018 01:15 AM 4 133 Mar 28, 2018 01:15 AM
Last Post: PDGLOBAL
[Javascript]  by dahnnDec 7, 2015 06:20 PM 0 105 Dec 7, 2015 06:20 PM
Last Post: dahnn
[PHP] by dynfoMar 25, 2011 08:14 PM 0 77 Mar 25, 2011 08:14 PM
Last Post: dynfo