JavaScriptencode + decode
function encodeEfIccid(value) {
const digits = value.replace(/[\s-]/g, "");
if (!/^[0-9]{18,20}$/.test(digits)) throw new TypeError("18-20 digits required");
return digits.padEnd(20, "F").match(/../g)
.map(([a, b]) => b + a).join("");
}
function decodeEfIccid(value) {
const hex = value.trim().replace(/^0x/i, "")
.replace(/[\s:-]/g, "").toUpperCase();
if (!/^[0-9A-F]{20}$/.test(hex)) throw new TypeError("10 hex bytes required");
const swapped = hex.match(/../g)
.map(([a, b]) => b + a).join("");
if (!/^[0-9]{18,20}F{0,2}$/.test(swapped)) throw new TypeError("Invalid BCD or filler");
return swapped.replace(/F+$/, "");
}
import re
def encode_ef_iccid(value: str) -> str:
digits = re.sub(r"[\s-]", "", value)
if not re.fullmatch(r"[0-9]{18,20}", digits):
raise ValueError("18-20 digits required")
padded = digits.ljust(20, "F")
return "".join(b + a for a, b in zip(padded[::2], padded[1::2]))
def decode_ef_iccid(value: str) -> str:
cleaned = re.sub(r"^0x", "", value.strip(), flags=re.IGNORECASE)
encoded = re.sub(r"[\s:-]", "", cleaned).upper()
if not re.fullmatch(r"[0-9A-F]{20}", encoded):
raise ValueError("10 hex bytes required")
swapped = "".join(b + a for a, b in zip(encoded[::2], encoded[1::2]))
if not re.fullmatch(r"[0-9]{18,20}F{0,2}", swapped):
raise ValueError("Invalid BCD or filler")
return swapped.rstrip("F")