If you are using XeTeX, here is some (slightly obfuscated) code to pack (\p
) pairs of Ascii characters into one Unicode character, storing the result in \toks0
(token register, which I called \|
below), or unpack (\u
) the result.
The encoding uses the fact that visible ascii characters have a character code (acessed with \number`
) between 32
(space) and 127
(tilde), so they can be stored in two digits by shifting by 32
. Four digits are then packed in one character by adding 10000
(minus the two shifts of 32
, hence 6768
, minus a 32
hidden in the definition of \.
, hence 6736
), to ensure that this gives 5 digits (starting with 1).
Decoding reads characters one at a time, through \number`
, which gives 1
, followed by two pairs of digits. We add 32
to each pair, set the uppercase code of .
to that value, and store \uppercase{.}
in \|
.
Stopping the loop is done by inserting \iffalse
at the right place in both packing and unpacking.
% Packing 2 ascii char per Unicode char with XeTeX.
\begingroup
\toksdef\|0
\let\ea\expandafter
\def\>{\uppercase{\|\ea{\the\|.}}}
\def\.{\uccode`.\numexpr32+}
\def\p#1{\|{}\ea\q#1\ {\ \iffalse}\ \fi\relax}
\def\q#1#2{\.\number`#100+`#2+6736\>\q}
\def\u#1{\|{}\ea\v\number`#1 \^^J{\iffalse}..\fi\relax}
\def\v1#1#2#3#4{\.#1#2\>\.#3#4\>\ea\v\number`}
% Example
\p{The\ main\ author\ is\ Seamus!}\showthe\|
\ea\u\ea{\the\|}\showthe\|
\endgroup
\csname @@end\endcsname\end
The group is there to avoid leaking out the definitions. Of course, you probably want to only leave the definitions for \u
, and write \u{㮨䈄䃉䠨䘾❙䝼㭁䃁䢗❴}\message{\the\|}
in your document.