Converting ISO Country Codes to Flag Emojis – Dan Q
Skip to content
Today I learned something that is probably already well-known in some circles… but I hadn’t noticed it before and it made me go “wow”:
There’s a really simple algorithm for converting ISO 3166-1 alpha-2 country codes into the emoji representations<br>of the flags of those countries .
I made an interactive to show how it works (enter a two-letter country code!). There’s a longer explanation below:
Here’s the essence of the algorithm:
Take the two-letter country code, e.g. FR for France.
Get the character code of the uppercase variant of each letter: so F becomes 70 and R becomes<br>82.1
Add 127,397 to each of them, so now F is 127,467 and R 127,479.
Render the unicode characters at those codepoints: F turns into 🇫 and R turns into 🇷.
Concatenate those characters and you get the emoji of the flag: 🇫🇷
I’ve often find things that are wonderfully clever about Unicode, but this might be my new favourite.
func countryEmojiFlag(countryCode string) string {<br>cc := strings.ToUpper(strings.TrimSpace(countryCode))<br>if len(cc) != 2 || cc[0] 'A' || cc[0] > 'Z' || cc[1] 'A' || cc[1] > 'Z' {<br>return ""<br>return string([]rune{rune(cc[0]) + 127397, rune(cc[1]) + 127397})
My actual implementation was Go, rather than JavaScript2, as part of a side project<br>this weekend. Here’s the function I came up with.
Today was also the day that I discovered that while SU is a reserved 2-letter ISO 3166-1 designation for the Soviet Union, the flag of the USSR is not a<br>registered emoji. But if it were, we can work out what codepoint it’d be at! So I can type this – 🇸🇺 – here, safe in the knowledge that if that emoji comes to exist in the<br>future, then you’ll be able to revisit this blog post and see it!
You know what: there might be a game in these country codes and their flags somewhere. Like: a game where you have to get from one country to another: like, say, from the 🇨🇰 Cook<br>Islands (CK) to 🇧🇯 Benin (BJ). But you’re only allowed to change one letter at a time and you have to land in a real country. I think the fastest route between those two takes three<br>steps, e.g. 🇨🇰 Cook Islands (CK) to 🇹🇰 Tokelau (TK) to 🇹🇯 Tajikstan (TJ) to 🇧🇯 Benin (BJ)… It’s probably a bit easy though: I haven’t yet found any that require more than three moves<br>and most can be done in just two.
It gets a lot harder if you require letters to only be changed to an adjacent letter, but this variant makes some permutations impossible. Maybe there’s an optimisation puzzle in the<br>style of the Travelling Salesman problem? Or maybe by mixing in geographical restrictions such as an inability to visit a certain continent that would make it more challenging<br>and fun? Just brainstorming here…
Footnotes
1 An alternative way of thinking about it is that you’re taking the number of the letter<br>in the alphabet – e.g F=6, R=18 – and adding 64 to each. Here’s why, and why it’s beautiful.
2 I don’t get to write Go often, and I seem to get rusty at it quickly, but I enjoy the<br>feeling of writing something so raw and yet so clean.
Reactions
👍 0
🤔 0
😮 1
🇪🇺 1
No time to comment? Send an emoji with just one click!
1 comment
Tulip says:
Yess, I love these weird combo characters for unicode, but it leaves things in an odd space where device manufacturers or emoji font creators decide for themselves which countries<br>and flags they recognise?
I would be curious if there’s a list of all the differences in their flag supports and such.
I certainly remember trying to push for Openmoji to display the True South flag for Antarctica, as it’s actually the flag flown by researchers there, rather than the old Bartram<br>flag that was made by some British vexillologist for some CDROM encyclopedia…
There was also a unicode proposal for a set of digits 0-9 which would combine to form wikidata IDs, then emoji fonts could basically choose to implement emoji for any object or<br>concept that exists, by building the wikidata ID up. Alas it got rejected, as it’s very silly. But I kind of wish it got accepted, also because it is very silly, and highlights<br>some of the oddities in cross-platform emoji support.
31 May, 2026, 14:05
Reply here Cancel reply
Your email address will not be published. Required fields are marked *
Comment *
Name *
Email *
Website
Remember me?
Reply on your own site
If you post a reply on your own site and it doesn't show up automatically, give me the link:
Reply elsewhere
You can reply to this post on
Mastodon (@blog@danq.me),
Mastodon (@dan@danq.me).
Reply by email
I'd love to hear what you think. Send an email to<br>b29328@danq.me; be sure to let me know if you're happy for your comment to appear on the Web!