2

I want to get the country prefix for phone numbers.

e.g. if I am in the US and I enter the number 0123 - 456, I want to get the prefix +1 or 001, so the number in the end is (+1) 0123 - 456 If I am in Germany the same phone number would be (+49) 0123 - 456.

How can I do this in a UWP app? I can't find anything in the globalization or cultureinfo namespace.

user3079834
  • 2,009
  • 2
  • 31
  • 63
  • The leading `+` is easy, that's a global standard. I'm pretty sure that `0123-456` isn't a valid US number to start with, and considering the complexity of _dial plans_ you really don't want to include invalid numbers in the discussion. – MSalters Jan 17 '16 at 10:53
  • This is just for example purpose, it doesn't matter if it is valid. I just want to get the `+` prefix number. Also I do not know the number format in the US, and the phone number for germany is in an invalid format to, both is irrelevant to my question. I really just need the + prefix, e.g. `+1` if my user is in the US (or his device is in US settings) and `+49`, when in Germany or `+33` when in France – user3079834 Jan 17 '16 at 10:58
  • Why do you claim that "it doesn't matter" ? You have to understand that a phone number is rewritten quite a few times in order to make a call. For instance, the `+49` is stripped off when the call is handed off to a German gateway phone switch. Makes sense, because the `+49` was needed to pick that phone switch. It's no longer needed. The problem with invalid numbers is that the originating network may reject them straight away. (The `+49` invalid example probably will be rejected by the German gateway, though - the originating network won't know the German dial plan) – MSalters Jan 17 '16 at 11:05
  • I do understand that. But I do not understand on how you complaining, that my arbitrary imaginary example numbers, that I use to support my question do help with my problem at all. The user will type in his or her own number. If they are wrong, it is up to the user, I just want to support adding the country code, if he or she did not do that. If the suggested country code is wrong, the user can change it. If the user enters a wrong number, the user can change it. **Solution** to the problem in next post. – user3079834 Jan 17 '16 at 11:16

2 Answers2

3

In this post the prefix country number is retrieved by a dictionary using the ISO 3166-1 2-code.

Some copy-pasta and little language changes later, we have the following code:

private string getCountryCode(string ISO3166)
{
    var dictionary = new Dictionary<string, string>();

    dictionary.Add("AC", "+247");
    dictionary.Add("AD", "+376");
    dictionary.Add("AE", "+971");
    dictionary.Add("AF", "+93");
    dictionary.Add("AG", "+1-268");
    dictionary.Add("AI", "+1-264");
    dictionary.Add("AL", "+355");
    dictionary.Add("AM", "+374");
    dictionary.Add("AN", "+599");
    dictionary.Add("AO", "+244");
    dictionary.Add("AR", "+54");
    dictionary.Add("AS", "+1-684");
    dictionary.Add("AT", "+43");
    dictionary.Add("AU", "+61");
    dictionary.Add("AW", "+297");
    dictionary.Add("AX", "+358-18");
    dictionary.Add("AZ", "+994"); // or +374-97
    dictionary.Add("BA", "+387");
    dictionary.Add("BB", "+1-246");
    dictionary.Add("BD", "+880");
    dictionary.Add("BE", "+32");
    dictionary.Add("BF", "+226");
    dictionary.Add("BG", "+359");
    dictionary.Add("BH", "+973");
    dictionary.Add("BI", "+257");
    dictionary.Add("BJ", "+229");
    dictionary.Add("BM", "+1-441");
    dictionary.Add("BN", "+673");
    dictionary.Add("BO", "+591");
    dictionary.Add("BR", "+55");
    dictionary.Add("BS", "+1-242");
    dictionary.Add("BT", "+975");
    dictionary.Add("BW", "+267");
    dictionary.Add("BY", "+375");
    dictionary.Add("BZ", "+501");
    dictionary.Add("CA", "+1");
    dictionary.Add("CC", "+61");
    dictionary.Add("CD", "+243");
    dictionary.Add("CF", "+236");
    dictionary.Add("CG", "+242");
    dictionary.Add("CH", "+41");
    dictionary.Add("CI", "+225");
    dictionary.Add("CK", "+682");
    dictionary.Add("CL", "+56");
    dictionary.Add("CM", "+237");
    dictionary.Add("CN", "+86");
    dictionary.Add("CO", "+57");
    dictionary.Add("CR", "+506");
    dictionary.Add("CS", "+381");
    dictionary.Add("CU", "+53");
    dictionary.Add("CV", "+238");
    dictionary.Add("CX", "+61");
    dictionary.Add("CY", "+357"); // or +90-392
    dictionary.Add("CZ", "+420");
    dictionary.Add("DE", "+49");
    dictionary.Add("DJ", "+253");
    dictionary.Add("DK", "+45");
    dictionary.Add("DM", "+1-767");
    dictionary.Add("DO", "+1-809"); // and 1-829?
    dictionary.Add("DZ", "+213");
    dictionary.Add("EC", "+593");
    dictionary.Add("EE", "+372");
    dictionary.Add("EG", "+20");
    dictionary.Add("EH", "+212");
    dictionary.Add("ER", "+291");
    dictionary.Add("ES", "+34");
    dictionary.Add("ET", "+251");
    dictionary.Add("FI", "+358");
    dictionary.Add("FJ", "+679");
    dictionary.Add("FK", "+500");
    dictionary.Add("FM", "+691");
    dictionary.Add("FO", "+298");
    dictionary.Add("FR", "+33");
    dictionary.Add("GA", "+241");
    dictionary.Add("GB", "+44");
    dictionary.Add("GD", "+1-473");
    dictionary.Add("GE", "+995");
    dictionary.Add("GF", "+594");
    dictionary.Add("GG", "+44");
    dictionary.Add("GH", "+233");
    dictionary.Add("GI", "+350");
    dictionary.Add("GL", "+299");
    dictionary.Add("GM", "+220");
    dictionary.Add("GN", "+224");
    dictionary.Add("GP", "+590");
    dictionary.Add("GQ", "+240");
    dictionary.Add("GR", "+30");
    dictionary.Add("GT", "+502");
    dictionary.Add("GU", "+1-671");
    dictionary.Add("GW", "+245");
    dictionary.Add("GY", "+592");
    dictionary.Add("HK", "+852");
    dictionary.Add("HN", "+504");
    dictionary.Add("HR", "+385");
    dictionary.Add("HT", "+509");
    dictionary.Add("HU", "+36");
    dictionary.Add("ID", "+62");
    dictionary.Add("IE", "+353");
    dictionary.Add("IL", "+972");
    dictionary.Add("IM", "+44");
    dictionary.Add("IN", "+91");
    dictionary.Add("IO", "+246");
    dictionary.Add("IQ", "+964");
    dictionary.Add("IR", "+98");
    dictionary.Add("IS", "+354");
    dictionary.Add("IT", "+39");
    dictionary.Add("JE", "+44");
    dictionary.Add("JM", "+1-876");
    dictionary.Add("JO", "+962");
    dictionary.Add("JP", "+81");
    dictionary.Add("KE", "+254");
    dictionary.Add("KG", "+996");
    dictionary.Add("KH", "+855");
    dictionary.Add("KI", "+686");
    dictionary.Add("KM", "+269");
    dictionary.Add("KN", "+1-869");
    dictionary.Add("KP", "+850");
    dictionary.Add("KR", "+82");
    dictionary.Add("KW", "+965");
    dictionary.Add("KY", "+1-345");
    dictionary.Add("KZ", "+7");
    dictionary.Add("LA", "+856");
    dictionary.Add("LB", "+961");
    dictionary.Add("LC", "+1-758");
    dictionary.Add("LI", "+423");
    dictionary.Add("LK", "+94");
    dictionary.Add("LR", "+231");
    dictionary.Add("LS", "+266");
    dictionary.Add("LT", "+370");
    dictionary.Add("LU", "+352");
    dictionary.Add("LV", "+371");
    dictionary.Add("LY", "+218");
    dictionary.Add("MA", "+212");
    dictionary.Add("MC", "+377");
    dictionary.Add("MD", "+373"); // or +373-533
    dictionary.Add("ME", "+382");
    dictionary.Add("MG", "+261");
    dictionary.Add("MH", "+692");
    dictionary.Add("MK", "+389");
    dictionary.Add("ML", "+223");
    dictionary.Add("MM", "+95");
    dictionary.Add("MN", "+976");
    dictionary.Add("MO", "+853");
    dictionary.Add("MP", "+1-670");
    dictionary.Add("MQ", "+596");
    dictionary.Add("MR", "+222");
    dictionary.Add("MS", "+1-664");
    dictionary.Add("MT", "+356");
    dictionary.Add("MU", "+230");
    dictionary.Add("MV", "+960");
    dictionary.Add("MW", "+265");
    dictionary.Add("MX", "+52");
    dictionary.Add("MY", "+60");
    dictionary.Add("MZ", "+258");
    dictionary.Add("NA", "+264");
    dictionary.Add("NC", "+687");
    dictionary.Add("NE", "+227");
    dictionary.Add("NF", "+672");
    dictionary.Add("NG", "+234");
    dictionary.Add("NI", "+505");
    dictionary.Add("NL", "+31");
    dictionary.Add("NO", "+47");
    dictionary.Add("NP", "+977");
    dictionary.Add("NR", "+674");
    dictionary.Add("NU", "+683");
    dictionary.Add("NZ", "+64");
    dictionary.Add("OM", "+968");
    dictionary.Add("PA", "+507");
    dictionary.Add("PE", "+51");
    dictionary.Add("PF", "+689");
    dictionary.Add("PG", "+675");
    dictionary.Add("PH", "+63");
    dictionary.Add("PK", "+92");
    dictionary.Add("PL", "+48");
    dictionary.Add("PM", "+508");
    dictionary.Add("PR", "+1-787"); // and 1-939 ?
    dictionary.Add("PS", "+970");
    dictionary.Add("PT", "+351");
    dictionary.Add("PW", "+680");
    dictionary.Add("PY", "+595");
    dictionary.Add("QA", "+974");
    dictionary.Add("RE", "+262");
    dictionary.Add("RO", "+40");
    dictionary.Add("RS", "+381");
    dictionary.Add("RU", "+7");
    dictionary.Add("RW", "+250");
    dictionary.Add("SA", "+966");
    dictionary.Add("SB", "+677");
    dictionary.Add("SC", "+248");
    dictionary.Add("SD", "+249");
    dictionary.Add("SE", "+46");
    dictionary.Add("SG", "+65");
    dictionary.Add("SH", "+290");
    dictionary.Add("SI", "+386");
    dictionary.Add("SJ", "+47");
    dictionary.Add("SK", "+421");
    dictionary.Add("SL", "+232");
    dictionary.Add("SM", "+378");
    dictionary.Add("SN", "+221");
    dictionary.Add("SO", "+252");
    dictionary.Add("SR", "+597");
    dictionary.Add("ST", "+239");
    dictionary.Add("SV", "+503");
    dictionary.Add("SY", "+963");
    dictionary.Add("SZ", "+268");
    dictionary.Add("TA", "+290");
    dictionary.Add("TC", "+1-649");
    dictionary.Add("TD", "+235");
    dictionary.Add("TG", "+228");
    dictionary.Add("TH", "+66");
    dictionary.Add("TJ", "+992");
    dictionary.Add("TK", "+690");
    dictionary.Add("TL", "+670");
    dictionary.Add("TM", "+993");
    dictionary.Add("TN", "+216");
    dictionary.Add("TO", "+676");
    dictionary.Add("TR", "+90");
    dictionary.Add("TT", "+1-868");
    dictionary.Add("TV", "+688");
    dictionary.Add("TW", "+886");
    dictionary.Add("TZ", "+255");
    dictionary.Add("UA", "+380");
    dictionary.Add("UG", "+256");
    dictionary.Add("US", "+1");
    dictionary.Add("UY", "+598");
    dictionary.Add("UZ", "+998");
    dictionary.Add("VA", "+379");
    dictionary.Add("VC", "+1-784");
    dictionary.Add("VE", "+58");
    dictionary.Add("VG", "+1-284");
    dictionary.Add("VI", "+1-340");
    dictionary.Add("VN", "+84");
    dictionary.Add("VU", "+678");
    dictionary.Add("WF", "+681");
    dictionary.Add("WS", "+685");
    dictionary.Add("YE", "+967");
    dictionary.Add("YT", "+262");
    dictionary.Add("ZA", "+27");
    dictionary.Add("ZM", "+260");
    dictionary.Add("ZW", "+263");

    if (dictionary.ContainsKey(ISO3166))
    {
      return dictionary[ISO3166];
    }
    else
    {
      return null;
    }
}

If somebody knows a more elegant way to retrieve these numbers or if there is an official API for that, please feel free to share it.

Use it like this:

private string getCountryCode()
{
    var c = CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
    return getCountryCode(c.ToUpper());
}
user3079834
  • 2,009
  • 2
  • 31
  • 63
1

The phone's own country code is part of the ICCID, starting after the prefix 89. No need for a table.

Community
  • 1
  • 1
MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Thank you very much for the answer, unfortunately in UWP (universal windows programs) one does not have access to the ICCID without special permissions. – user3079834 Jan 17 '16 at 11:32
  • @user3079834: Apologies, I got sidetracked when following the link in your answer. – MSalters Jan 17 '16 at 11:35
  • no problem, for android your solution seems to be quite elegant. I wouldn't have thought of the ICCID. That would solve the problem that persists in my code, when I am in France, but my settings are set to US settings (because I have an English keyboard and prefer programs in english as default), so my code would return `US` as `TwoLetterISOLanguageName` and therefore +1 – user3079834 Jan 17 '16 at 11:43
  • @user3079834: Basically you need the SIM details, not UI settings. The phone network uses only the SIM details. – MSalters Jan 18 '16 at 00:37