{packid:unpacked_id}

Description

Converts an unpacked id - a 32-character hexadecimal string, the human-readable long form of an AA id - to its packed binary form, the 16-byte representation AA stores internally. It is the template counterpart of pack_id() and the inverse of unpack. The output is raw binary bytes, not printable text, so packid is almost never used on its own; the canonical pattern is to feed it into an encoder, as in base64 of packid of md5 of some string, then take a short slice with substr, to build a compact stable token such as a verification code or a deduplication key. Two edge cases: an empty input returns empty, and the literal 0 also returns empty, because PHP treats the string 0 as false. And because the template path is not binary-safe, any packed byte above 0x7F is replaced by a question mark, so the value only round-trips or base64-encodes faithfully when every byte stays in low ASCII; for ordinary ids and hashes the encoded result is stable within one install but is not the true binary encoding.

Parameters

unpacked_id required

The unpacked id to pack: a 32-character hexadecimal string (the long form of an AA id, or any hex string such as an md5 hash). Each pair of hex digits becomes one byte, so 32 hex characters produce 16 bytes. An odd trailing nibble is padded and non-hex characters are read as zero. Empty input, and the literal 0, both return empty.

Examples

test[{packid:}]
Expected[]
Actual[]
With no id to pack, packid returns an empty string. The brackets are only there to make the empty result visible.
test{base64:{packid:{md5:hello}}}
ExpectedXUFAKj9LKnY/cT8/EBfFkg==
ActualXUFAKj9LKnY/cT8/EBfFkg==
md5 of hello is 5d41402abc4b2a76b9719d911017c592. Packed and base64-encoded by PHP that would be XUFAKrxLKna5cZ2REBfFkg==, but AAs template path is not binary-safe: every packed byte above 0x7F is replaced by a question mark (0x3F) before base64 runs, so the result differs. It stays the same on every render of this install, which is why the token pattern still works as a stable key - just do not treat it as a faithful encoding of the hash.
test{base64:{packid:4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a}}
ExpectedSkpKSkpKSkpKSkpKSkpKSg==
ActualSkpKSkpKSkpKSkpKSkpKSg==
The raw output of packid is binary, so it is wrapped in base64 to get text you can store or display. Here every packed byte is 0x4A (the letter J), all within low ASCII, so the base64 matches what PHP would produce exactly.
test{strlen:{packid:5d41402abc4b2a76b9719d911017c592}}
Expected16
Actual16
packid turns each pair of hex digits into one byte, so a 32-character id packs to exactly 16 bytes. strlen confirms the size without printing the raw (non-text) bytes.
test{substr:{base64:{packid:{md5:newsletter}}}:2:8}
ExpectedM/P3sDbj
ActualM/P3sDbj
The canonical use: hash a string with md5, pack it, base64 it, then slice a few characters with substr to get a compact token - a verification code, an unsubscribe key, a dedup key. The token is stable within this install. Note it is not the true base64 of the md5 (see the next example).
test[{packid:0}]
Expected[]
Actual[]
A trap worth knowing: passing 0 returns empty, not a packed zero. PHP treats the string 0 as false, so packid takes its empty-input branch. The code comment promising a 0 result for input 0 is unreachable.