Skip to content

Instantly share code, notes, and snippets.

@BenjaminAdams
Last active May 21, 2024 20:18
Show Gist options
  • Save BenjaminAdams/277928c7ad291ea7aea8 to your computer and use it in GitHub Desktop.
Save BenjaminAdams/277928c7ad291ea7aea8 to your computer and use it in GitHub Desktop.
C# convert two letter country codes to three letter country codes
public static class CountryCodeConverter
{
public static string ConvertThreeLetterNameToTwoLetterName(string twoLetterCountryCode)
{
if (twoLetterCountryCode == null || twoLetterCountryCode.Length != 2)
{
throw new ArgumentException("name must be three letters.");
}
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (CultureInfo culture in cultures)
{
RegionInfo region = new RegionInfo(culture.LCID);
if (region.TwoLetterISORegionName.ToUpper() == twoLetterCountryCode.ToUpper())
{
return region.ThreeLetterISORegionName;
}
}
throw new ArgumentException("Could not get country code");
}
}
[TestClass]
public class CountryCodeConverterTest
{
[TestMethod]
public void Test_All_Country_Code_Conversion()
{
var countryCodes = new Dictionary<string, string>() {
{ "AFG", "AF" }, // Afghanistan
{ "ALB", "AL" }, // Albania
{ "ARE", "AE" }, // U.A.E.
{ "ARG", "AR" }, // Argentina
{ "ARM", "AM" }, // Armenia
{ "AUS", "AU" }, // Australia
{ "AUT", "AT" }, // Austria
{ "AZE", "AZ" }, // Azerbaijan
{ "BEL", "BE" }, // Belgium
{ "BGD", "BD" }, // Bangladesh
{ "BGR", "BG" }, // Bulgaria
{ "BHR", "BH" }, // Bahrain
{ "BIH", "BA" }, // Bosnia and Herzegovina
{ "BLR", "BY" }, // Belarus
{ "BLZ", "BZ" }, // Belize
{ "BOL", "BO" }, // Bolivia
{ "BRA", "BR" }, // Brazil
{ "BRN", "BN" }, // Brunei Darussalam
{ "CAN", "CA" }, // Canada
{ "CHE", "CH" }, // Switzerland
{ "CHL", "CL" }, // Chile
{ "CHN", "CN" }, // People's Republic of China
{ "COL", "CO" }, // Colombia
{ "CRI", "CR" }, // Costa Rica
{ "CZE", "CZ" }, // Czech Republic
{ "DEU", "DE" }, // Germany
{ "DNK", "DK" }, // Denmark
{ "DOM", "DO" }, // Dominican Republic
{ "DZA", "DZ" }, // Algeria
{ "ECU", "EC" }, // Ecuador
{ "EGY", "EG" }, // Egypt
{ "ESP", "ES" }, // Spain
{ "EST", "EE" }, // Estonia
{ "ETH", "ET" }, // Ethiopia
{ "FIN", "FI" }, // Finland
{ "FRA", "FR" }, // France
{ "FRO", "FO" }, // Faroe Islands
{ "GBR", "GB" }, // United Kingdom
{ "GEO", "GE" }, // Georgia
{ "GRC", "GR" }, // Greece
{ "GRL", "GL" }, // Greenland
{ "GTM", "GT" }, // Guatemala
{ "HKG", "HK" }, // Hong Kong S.A.R.
{ "HND", "HN" }, // Honduras
{ "HRV", "HR" }, // Croatia
{ "HUN", "HU" }, // Hungary
{ "IDN", "ID" }, // Indonesia
{ "IND", "IN" }, // India
{ "IRL", "IE" }, // Ireland
{ "IRN", "IR" }, // Iran
{ "IRQ", "IQ" }, // Iraq
{ "ISL", "IS" }, // Iceland
{ "ISR", "IL" }, // Israel
{ "ITA", "IT" }, // Italy
{ "JAM", "JM" }, // Jamaica
{ "JOR", "JO" }, // Jordan
{ "JPN", "JP" }, // Japan
{ "KAZ", "KZ" }, // Kazakhstan
{ "KEN", "KE" }, // Kenya
{ "KGZ", "KG" }, // Kyrgyzstan
{ "KHM", "KH" }, // Cambodia
{ "KOR", "KR" }, // Korea
{ "KWT", "KW" }, // Kuwait
{ "LAO", "LA" }, // Lao P.D.R.
{ "LBN", "LB" }, // Lebanon
{ "LBY", "LY" }, // Libya
{ "LIE", "LI" }, // Liechtenstein
{ "LKA", "LK" }, // Sri Lanka
{ "LTU", "LT" }, // Lithuania
{ "LUX", "LU" }, // Luxembourg
{ "LVA", "LV" }, // Latvia
{ "MAC", "MO" }, // Macao S.A.R.
{ "MAR", "MA" }, // Morocco
{ "MCO", "MC" }, // Principality of Monaco
{ "MDV", "MV" }, // Maldives
{ "MEX", "MX" }, // Mexico
{ "MKD", "MK" }, // Macedonia (FYROM)
{ "MLT", "MT" }, // Malta
{ "MNE", "ME" }, // Montenegro
{ "MNG", "MN" }, // Mongolia
{ "MYS", "MY" }, // Malaysia
{ "NGA", "NG" }, // Nigeria
{ "NIC", "NI" }, // Nicaragua
{ "NLD", "NL" }, // Netherlands
{ "NOR", "NO" }, // Norway
{ "NPL", "NP" }, // Nepal
{ "NZL", "NZ" }, // New Zealand
{ "OMN", "OM" }, // Oman
{ "PAK", "PK" }, // Islamic Republic of Pakistan
{ "PAN", "PA" }, // Panama
{ "PER", "PE" }, // Peru
{ "PHL", "PH" }, // Republic of the Philippines
{ "POL", "PL" }, // Poland
{ "PRI", "PR" }, // Puerto Rico
{ "PRT", "PT" }, // Portugal
{ "PRY", "PY" }, // Paraguay
{ "QAT", "QA" }, // Qatar
{ "ROU", "RO" }, // Romania
{ "RUS", "RU" }, // Russia
{ "RWA", "RW" }, // Rwanda
{ "SAU", "SA" }, // Saudi Arabia
{ "SCG", "CS" }, // Serbia and Montenegro (Former)
{ "SEN", "SN" }, // Senegal
{ "SGP", "SG" }, // Singapore
{ "SLV", "SV" }, // El Salvador
{ "SRB", "RS" }, // Serbia
{ "SVK", "SK" }, // Slovakia
{ "SVN", "SI" }, // Slovenia
{ "SWE", "SE" }, // Sweden
{ "SYR", "SY" }, // Syria
{ "TAJ", "TJ" }, // Tajikistan
{ "THA", "TH" }, // Thailand
{ "TKM", "TM" }, // Turkmenistan
{ "TTO", "TT" }, // Trinidad and Tobago
{ "TUN", "TN" }, // Tunisia
{ "TUR", "TR" }, // Turkey
{ "TWN", "TW" }, // Taiwan
{ "UKR", "UA" }, // Ukraine
{ "URY", "UY" }, // Uruguay
{ "USA", "US" }, // United States
{ "UZB", "UZ" }, // Uzbekistan
{ "VEN", "VE" }, // Bolivarian Republic of Venezuela
{ "VNM", "VN" }, // Vietnam
{ "YEM", "YE" }, // Yemen
{ "ZAF", "ZA" }, // South Africa
{ "ZWE", "ZW" }, // Zimbabwe
};
foreach (var cnt in countryCodes)
{
Assert.AreEqual(CountryCodeConverter.ConvertThreeLetterNameToTwoLetterName(cnt.Value), cnt.Key);
}
}
}
@cajuncoding
Copy link

Nice comment @mombrea it's helpful for those that find our way here 👍

@cajuncoding
Copy link

cajuncoding commented May 21, 2024

For anyone finding their way here looking to map three letter country codes to two letter codes, here's a streamlined adaptation of this gist as a Static Helper class incorporating the fix noted by @mombrea above along with faster lookup caching:

https://gist.github.com/cajuncoding/69422463cf74f8162ec25d9149ce91a8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment