9

I am running the AT command AT+KCELL to get cell information and it returns, amongst other things, a PLMN (Public Land and Mobile Network) - the description of this from the documentation is:

PLMN identifiers (3 bytes), made of MCC (Mobile Country Code), and MNC (Mobile Network Code).

OK, that matches what Wikipedia says - in there is the MCC and MNC. Now what I don't understand is how do I extract the aforementioned MCC and MNC values?

Here is an example. I get back:

32f210

and I am told (though I am sceptical) that that should result in:

MNC: 1
MCC: 232

but I can't for the life of me work out how to get that result from the PLMN so how do I parse this?

kmp
  • 10,535
  • 11
  • 75
  • 125

2 Answers2

14

Well, I have found this out and figured I would add an answer here in case there is some other unlucky soul who has to do this - the PDF called GSM Technical Specification (section 10.2.4) contains the answer, the relevant bit is:

PLMN Contents: Mobile Country Code (MCC) followed by the Mobile Network Code (MNC). Coding: according to TS GSM 04.08 [14].

  • If storage for fewer than the maximum possible number n is required, the excess bytes shall be set to 'FF'. For instance, using 246 for the MCC and 81 for the MNC and if this is the first and only PLMN, the contents reads as follows: Bytes 1-3: '42' 'F6' '18' Bytes 4-6: 'FF' 'FF' 'FF' etc.

So I was wrong to be sceptical!

I need to read from the left swapping the digits around so the first two bytes would be the MCC so that would be 232f and the MNC would be 01 then I just discard the f and I have 232 and 1! Glad that one is sorted.

For example, in c# you can do it like this:

string plmn = "whatever the plmn is";

string mcc = new string(plmn.Substring(0, 2).Reverse().ToArray())
    + new string(plmn.Substring(2, 2).Reverse().ToArray())
    .Replace('f', ' ')
    .Trim();

string mnc = new string(plmn.Substring(4, 2).Reverse().ToArray())
    .Replace('f', ' ')
    .Trim();
kmp
  • 10,535
  • 11
  • 75
  • 125
  • 7
    In fact MCC takes 3 nibbles, and MNC takes 3 nibbles (US operators have 3-digit MNC). And it's not a good idea to save MCC/MNC as numbers, as leading zeroes matter. So first we have to swap nibbles of PLMN, `32f210 => 232f01`, then we assign `MCC=232` and `MNC=01` (`f` is ignored). But if we would have operator having 3-digit MNC, then we would have the following: `PLMN=130203 => 312030 => MCC=312, MNC=030`. – Ulugbek Umirov Jun 25 '14 at 13:00
2

Here is a java answer with bitwise operations:

public static String mcc(byte[] plmnId) {
  if (plmnId == null || plmnId.length != 3)
    throw new IllegalArgumentException(String.format("Wrong plmnid %s", Arrays.toString(plmnId)));

  int a1 = plmnId[0] & 0x0F;
  int a2 = (plmnId[0] & 0xF0) >> 4;
  int a3 = plmnId[1] & 0x0F;

  return "" + a1 + a2 + a3;
}

public static String mnc(byte[] plmnId) {
  if (plmnId == null || plmnId.length != 3)
    throw new IllegalArgumentException(String.format("Wrong plmnid %s", Arrays.toString(plmnId)));

  int a1 = plmnId[2] & 0x0F;
  int a2 = (plmnId[2] & 0xF0) >> 4;
  int a3 = (plmnId[1] & 0xF0) >> 4;

  if (a3 == 15)
    return "" + a1 + a2;
  else
    return "" + a1 + a2 + a3;
}
George
  • 7,206
  • 8
  • 33
  • 42