Converting country codes in .NET
Looks like there is no built-in way to do that. You can use the approach from the GenericTypeTea's answer. But if you need to convert many country codes you can avoid Cultures enumeration and pre-compute a mapping dictionary for country codes conversion.
That's how such generator looks like:
namespace ISOCountryCodes
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
class Program
{
public static void Main(string[] args)
{
var countryCodesMapping = new Dictionary<string, RegionInfo>();
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (var culture in cultures)
{
try
{
var region = new RegionInfo(culture.LCID);
countryCodesMapping[region.ThreeLetterISORegionName] = region;
}
catch (CultureNotFoundException)
{
var consoleColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Culture not found: " + culture.Name);
Console.ForegroundColor = consoleColor;
}
}
Console.WriteLine("var countryCodesMapping = new Dictionary<string, string>() {");
foreach (var mapping in countryCodesMapping.OrderBy(mapping => mapping.Key))
{
Console.WriteLine(" {{ \"{0}\", \"{1}\" }}, // {2}", mapping.Key, mapping.Value.TwoLetterISORegionName, mapping.Value.EnglishName);
}
Console.WriteLine("};");
}
}
}
Run this generator to get a country codes mapping dictionary like this:
var countryCodesMapping = 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
};
The RegionInfo
class does know the three-letter code (in the ThreeLetterISORegionName
property), but I don’t think there is a way to get RegionInfo
based on this code, you would need to enumerate all regions and add them to your own dictionary, with the three-letter code as a key.
However, I think the .NET Framework uses RegionInfo
to work with cultures, not countries in the ISO 3166-1 sense. Therefore, many countries from the ISO 3166-1 standard are not available (try e.g. SX
). I guess you should create your own country codebook.
Edit: From 246 countries in my current country codebook, RegionInfo
is available for 125 of them, the rest (121) are not supported. Conclusion: This is not a good way to get a country codebook.
UPDATED:
Didn't read the question properly before. The following should now be correctly converting from ISO 3166-1 alpha-3 to ISO 3166-1 alpha-2:
There's no built in way of doing it. You'll need to iterate through the CultureInfo
s to get the RegionInfo
s in order to manually find the match (this is pretty ineffecient so some caching would be advisable):
public string ConvertThreeLetterNameToTwoLetterName(string name)
{
if (name.Length != 3)
{
throw new ArgumentException("name must be three letters.");
}
name = name.ToUpper();
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (CultureInfo culture in cultures)
{
RegionInfo region = new RegionInfo(culture.LCID);
if (region.ThreeLetterISORegionName.ToUpper() == name)
{
return region.TwoLetterISORegionName;
}
}
return null;
}