diff --git a/pySim/utils.py b/pySim/utils.py index b49a437f..d75a000c 100644 --- a/pySim/utils.py +++ b/pySim/utils.py @@ -109,8 +109,8 @@ def enc_iccid(iccid): def enc_plmn(mcc, mnc): """Converts integer MCC/MNC into 3 bytes for EF""" if len(mnc) == 2: - mnc = "F%s" % mnc - return swap_nibbles("%s%s" % (mcc, mnc)) + mnc += "F" # pad to 3 digits if needed + return (mcc[1] + mcc[0]) + (mnc[2] + mcc[2]) + (mnc[1] + mnc[0]) def dec_spn(ef): byte1 = int(ef[0:2]) diff --git a/tests/test_utils.py b/tests/test_utils.py index 44fe631a..0df205ae 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -29,6 +29,16 @@ class DecTestCase(unittest.TestCase): def testDecMNCfromPLMN_unused(self): self.assertEqual(utils.dec_mnc_from_plmn("00f0ff"), 4095) + def test_enc_plmn(self): + with self.subTest("2-digit MCC"): + self.assertEqual(utils.enc_plmn("001", "01F"), "00F110") + self.assertEqual(utils.enc_plmn("001", "01"), "00F110") + self.assertEqual(utils.enc_plmn("295", "10"), "92F501") + + with self.subTest("3-digit MCC"): + self.assertEqual(utils.enc_plmn("001", "001"), "001100") + self.assertEqual(utils.enc_plmn("302", "361"), "031263") + def testDecAct_noneSet(self): self.assertEqual(utils.dec_act("0000"), [])