ISO Country Code to Unicode Flag in C#
Posted on July 22nd, 2018 in cloud-software
The Unicode 6.0 spec defines Regional Indicator symbols, which includes a set of flags. Here are the founders of NATO, if your system supports the flag characters (Windows 10 doesn't yet):
🇧🇪 🇨🇦 🇩🇰 🇫🇷 🇮🇸 🇮🇹 🇱🇺 🇳🇱 🇳🇴 🇵🇹 🇬🇧 🇺🇸
To create a flag, take the country ISO code (for example GB), and offset it to the flags Unicode block. See here for the JavaScript implementation, which I translated to C#.
Code
public static string IsoCountryCodeToFlagEmoji(this string country)
{
return string.Concat(country.ToUpper().Select(x => char.ConvertFromUtf32(x + 0x1F1A5)));
}
Usage
string gb = "gb".IsoCountryCodeToFlagEmoji(); // 🇬🇧
string fr = "fr".IsoCountryCodeToFlagEmoji(); // 🇫🇷