Debian Codegraph @

texmf-dist/source/generic/kastrup/binhex.dtx

raw
1 % \iffalse
2 % Copyright(c) 2001 by David Kastrup
3 % Any use of the code is permitted as long as this copyright notice is
4 % preserved in the code.
5 %
6 % In case of an emergency (no LaTeX available), you may rename this
7 % file to binhex.tex. In all other cases, use the accompanying
8 % binhex.ins in order to extract binhex.drv (run LaTeX on it for
9 % getting the documentation) and binhex.tex. If binhex.ins is
10 % missing, you can regenerate it by running docstrip on this file with
11 % the option ``installer'' set.
12 % \fi
13 % \CheckSum{251}
14 % \title{The \texttt{binhex.tex} package for expansible conversion
15 % into binary-based number systems}
16 % \author{David Kastrup\thanks
17 % {[email protected]}}
18 % \maketitle
19 % \tableofcontents
20 %
21 % \section{Usage}
22 % This is a file for expandably converting numbers into binary, octal
23 % and hexadecimal. All constructs \TeX\ accepts as an argument to its
24 % |\number| primitive are valid. This holds for all numeric arguments
25 % of the macros presented in here.
26 %
27 % You use this package by simply inputting it with
28 % \begin{verbatim}
29 %\input binhex
30 % \end{verbatim}
31 % It will work equally well under \LaTeX\ and plain \TeX. It does not
32 % even use plain \TeX, but only \TeX\ primitives. Simply setting the
33 % correct |\catcode| values for |{}#| and end of line will make it
34 % load and work under ini\TeX.
35 %
36 % The following macros are defined:
37 % \DescribeMacro{\binary}
38 % |\binary{|\meta{number}|}| will convert \meta{number} into its binary
39 % representation.
40 % \samples\binary{0}{\maxdimen}{-"7EE6}.
41 %
42 % \DescribeMacro{\nbinary}
43 % |\nbinary{|\meta{size}|}{|\meta{number}|}| will convert \meta{number}
44 % into a binary representation of at least \meta{size} digits length,
45 % filling up with leading zeros where necessary. The |-| sign of
46 % negative numbers is not counted. If both \meta{size} and
47 % \meta{number} are zero, an empty string is generated. This should
48 % please some computer scientists in some situations.
49 % \samples{\nbinary{3}}{3}{-2}{-12}.
50 %
51 % \DescribeMacro{\hex}
52 % |\hex{|\meta{number}|}| converts \meta{number} into its hexadecimal
53 % representation, using uppercase letters.
54 % \samples\hex{34}{-4711}.
55 %
56 % \DescribeMacro{\nhex}
57 % |\nhex{|\meta{size}|}{|\meta{number}|}| will convert \meta{number}
58 % into a hexadecimal representation of at least \meta{size} digits length,
59 % filling up with leading zeros where necessary. The |-| sign of
60 % negative numbers is not counted. If both \meta{size} and
61 % \meta{number} are zero, an empty string is generated. This should
62 % please some computer scientists in some situations.
63 % \samples{\nhex{3}}{3}{-\maxdimen}.
64 %
65 % \DescribeMacro{\oct}
66 % |\oct{|\meta{number}|}| converts \meta{number} into its octal
67 % representation.
68 % \samples\oct{34}{-4711}.
69 %
70 % \DescribeMacro{\noct}
71 % |\noct{|\meta{size}|}{|\meta{number}|}| will do the right thing.
72 % \samples{\noct{3}}{13}{-\maxdimen}.
73 %
74 % \DescribeMacro{\tetra}
75 % |\tetra{|\meta{number}|}| is for people counting with arms and legs
76 % instead of fingers, or for quadrupeds.
77 % \samples\tetra{34}{-4711}.
78 %
79 % \DescribeMacro{\ntetra}
80 % |\ntetra{|\meta{size}|}{|\meta{number}|}| is for those of the same
81 % count which have minimum requirements.
82 % \samples{\ntetra{3}}{3}{-\maxdimen}.
83 %
84 % \DescribeMacro{\nbinbased}
85 % |\nbinbased{|\meta{logbase}|}{|\meta{size}|}{|\meta{number}|}| will
86 % convert \meta{number} into number base $2^{\text{\meta{logbase}}}$ and
87 % generate at least \meta{size} digits. Only supported values of
88 % \meta{logbase} are 1, 2, 3, 4. This is called by all other macros
89 % except of the faster binary conversion macros.
90 % \samples{\nbinbased{3}{3}}{13}{-\maxdimen}.
91 % \samples{\nbinbased{2}{4}}{13}{-\maxdimen}.
92 %
93 % That's it, have fun!
94 % \StopEventually{}
95 % \section{Implementation}
96 % Now the implementation. First save catcode of |@| and old contents of
97 % |\toks0| in |\toks0|, then make |@| a letter to enable internal
98 % macros.
99 % \begin{macrocode}
100 %<*style>
101 \edef\next{\toks0=%
102 {\catcode`\noexpand\@=\the\catcode`\@\toks0{\the\toks0}}%
103 }
104 \next
105 \catcode`\@11
106 % \end{macrocode}
107 %
108 % \subsection{Binary conversions}
109 % \subsubsection{Basics}
110 % \begin{macro}{\bb@00}
111 % \begin{macro}{\bb@01}
112 % \begin{macro}{...}
113 % \begin{macro}{\bb@19}
114 % We now define the work horse macros for conversion of binary
115 % commands. If numbers were allowed in macro names, one of those
116 % definitions would be
117 % \begin{verbatim}
118 %\def\bb@13#1{6\csname bb@1#1\endcsname}
119 % \end{verbatim}
120 % That is, the macro divides the decimal two-digit number (up to~19)
121 % embedded into its name by~2, spews out the result and starts up the
122 % next macro with the first digit of the name of \emph{that} being the
123 % remainder from its own division, and the second digit being the next
124 % following one.
125 % \begin{macrocode}
126 \def\next#1#2#3{\expandafter \def \csname bb@#1\endcsname##1%
127 {#2\csname bb@#3##1\endcsname}}
128 \next{00}00 \next{01}01 \next{02}10 \next{03}11
129 \next{04}20 \next{05}21 \next{06}30 \next{07}31
130 \next{08}40 \next{09}41 \next{10}50 \next{11}51
131 \next{12}60 \next{13}61 \next{14}70 \next{15}71
132 \next{16}80 \next{17}81 \next{18}90 \next{19}91
133 % \end{macrocode}
134 % \end{macro}
135 % \end{macro}
136 % \end{macro}
137 % \end{macro}
138 % \begin{macro}{\bb@0+}
139 % \begin{macro}{\bb@1+}
140 % Now we need to end such a conversion. For no particular reason at
141 % all (well, some sneakiness might explain it later), we use |+| as an
142 % end marker for the decimal digits. After the |+|, we collect the
143 % resulting binary digits, from last to first. So here are two macros
144 % to deal with the last digit produced. Simply tacks them on after
145 % replacing the gobbled |+|.
146 % \begin{macrocode}
147 \expandafter \def \csname bb@0+\endcsname {+0}
148 \expandafter \def \csname bb@1+\endcsname {+1}
149 % \end{macrocode}
150 % \end{macro}
151 % \end{macro}
152 %
153 % Now all that remains to be done is to initiate the process, and to
154 % end it again. The process ends when the decimal number reaches~0.
155 % Since we want to produce one digit even when starting out, and we
156 % check the end of recursion by comparing the result with~|0|, we have a
157 % problem here. We solve it by comparing two letters, and looking for
158 % the complete |0+| sequence, and we start out by prepending a trivial |0|
159 % before the number to convert, so that at the first run it will be |00|
160 % instead of |0| if the number in question is~0.
161 %
162 % \begin{macro}{(\binary)}
163 % \begin{macro}{(\bb@dobinary)}
164 % Actually, this is how we \emph{could} do things. The actual, slightly
165 % different version used can be shared by the other converters, but
166 % we'll keep this listed for reference.
167 % \begin{verbatim}
168 %\def\binary#1{\expandafter \bb@dobinary \expandafter 0\number#1+}
169 %\def\bb@dobinary#1#2{\if0#1\if+#2\bb@endbinary \fi\fi
170 % \expandafter \bb@dobinary \number \csname bb@0#1\endcsname #2}
171 % \end{verbatim}
172 % Notice that |\number| here serves multiple purposes. It will
173 % initiate expansion that will only be stopped once the macros
174 % generating the next binary digit will crank out a space or anything
175 % else stopping expansion. Its second purpose is to get rid of any
176 % leading zeros that might remain from the last loop through.
177 %
178 % \begin{macro}{\bb@endbinary}
179 % |\bb@endbinary| can
180 % be used as written, though. It scraps everything up to the tack
181 % mark |+|, leaving only the converted results accumulated behind
182 % that. Since this means scrapping |\fi\fi| as well, it reinserts
183 % it in order to properly finish the conditional.
184 % \begin{macrocode}
185 \def\bb@endbinary#1+{\fi\fi}
186 % \end{macrocode}
187 % \end{macro}
188 % \end{macro}
189 % \end{macro}
190 %
191 % \subsubsection{Negative numbers}
192 % We would like negative numbers to work, too. The semantics for
193 % defined field widths are unclear (so we basically just tack a |-| on
194 % and convert the remaining number to the full width). Since the sign
195 % of the number is easy to discern, further massaging of the number to
196 % replace leading zeros by spaces, insert |+| signs and similar folderol
197 % are easy to do and left as an exercise to the reader. We mostly
198 % implement this to have the conversion not fail on any number.
199 %
200 % \begin{macro}{\bb@0-}
201 % The following one-liner achieves that. Figure out why. It is easy.
202 % \begin{macrocode}
203 \expandafter \def \csname bb@0-\endcsname {0+-\bb@dobinary}
204 % \end{macrocode}
205 % \end{macro}
206 %
207 % \subsubsection{Minimum field widths}
208 % \begin{macro}{\bb@0m}
209 % \begin{macro}{\bb@1m}
210 % We often have the situation that we want to produce a number that is
211 % at least of a certain length. We specify this by tagging on an
212 % appropriate number of |m| characters to the decimal as sort of a
213 % format string. If we match such an~|m| at the end of the conversion,
214 % we produce a digit and remove one~|m|, leaving all other |m| intact (or
215 % whatever else happens to be before our |+| sign).
216 % \begin{macrocode}
217 \expandafter\def\csname bb@0m\endcsname#1+{#1+0}
218 \expandafter\def\csname bb@1m\endcsname#1+{#1+1}
219 % \end{macrocode}
220 % \end{macro}
221 % \end{macro}
222 %
223 % \begin{macro}{(\nbinary)}
224 % We call |\nbinary| with the number of digits as first argument, the number
225 % itself as second.
226 %
227 % This now becomes trivial:
228 % \begin{verbatim}
229 %\def \nbinary#1#2{%
230 % \binary{\number #2\romannumeral \number \number #1 000}%
231 %}
232 % \end{verbatim}
233 %
234 % Whoa, what about all these calls of |\number|? First notice the space
235 % after |#1|. We need that in case |#1| happens to be octal or whatever,
236 % since the |000| at the end is supposed to multiply by 1000 \emph{decimal}.
237 % If |#1| were something like |'13| (11 decimal), the last |\number| will
238 % expand |'13|, the |\number| before that will remove the blank we
239 % inserted in case it was not needed to finish the number of digits,
240 % we multiply by~1000 and turn this into a roman numeral consisting of
241 % the appropriate amount of |m| letters. The very first |\number| ensures
242 % that in case |#2| is a complete number without the need of trailing
243 % spaces, still |\romannumeral| will be expanded when |\binary| first sets
244 % sight on the whole thing.
245 % \end{macro}
246 %
247 % \begin{macro}{\nbinary}
248 % \begin{macro}{\binary}
249 % \begin{macro}{\bb@dobinary}
250 % Actually, since there is a certain logic to returning
251 % an empty string when 0 is to be converted into a number at least 0
252 % characters long, we redefine the stuff the other way round, |\binary|
253 % as a special case of |\nbinary|. We check the end by testing against
254 % |0| followed by anything but~|m|. This has reasons\dots
255 %
256 % \begin{macrocode}
257 \def\bb@dobinary#1#2{\if#10\if m\string#2\else\bb@endbinary\fi\fi
258 \expandafter\bb@dobinary\number\csname bb@0#1\endcsname#2}
259 \def\nbinary#1#2{\expandafter\bb@dobinary\number\number#2%
260 \romannumeral\number\number#1 000+}
261 \def\binary{\nbinary1}
262 % \end{macrocode}
263 % \end{macro}
264 % \end{macro}
265 % \end{macro}
266 %
267 % \subsection{Hexadecimal and the rest}
268 %
269 % Ok, stop with the small fry. Now we want to convert into
270 % hexadecimal and octal as well. We do this by first converting into
271 % binary, then into the wanted base whenever enough binary digits have
272 % accumulated. In the following, we will talk about hex digits
273 % exclusively for simplicity, even though they might indeed be octal
274 % instead.
275 %
276 % \begin{macro}{\bb@h0}
277 % \begin{macro}{\bb@h1}
278 % \begin{macro}{\bb@h10}
279 % \begin{macro}{...}
280 % \begin{macro}{\bb@h1111}
281 %
282 % The following defines the equivalents of
283 % \begin{verbatim}
284 %\def \bb@h110#1+{\bb@dohex #1+6}
285 % \end{verbatim}
286 % This is a macro that appends 6, the representation of the binary
287 % number in its name, after the tack mark |+|. So what's with the
288 % |\number+|? Actually, here it does nothing but disappear. We just
289 % write this to remind us of how the macros will be called. When the
290 % macros are called, we use the same construct, and then |\number|
291 % will disappear together with the~|+| (that we use as a tack mark)
292 % and take along any leading zeros. We drop leading zeros so that we
293 % can share the conversion macros for hex and octal (and quaternary?)
294 % where they overlap.
295 %
296 % \begin{macrocode}
297 \def \next #1#2{\expandafter \def
298 \csname bb@h\number +#1\endcsname ##1+{\bb@dohex ##1+#2}%
299 }
300 \next {0}0 \next {1}1 \next {10}2 \next {11}3
301 \next {100}4 \next {101}5 \next {110}6 \next {111}7
302 \next{1000}8 \next{1001}9 \next{1010}A \next{1011}B
303 \next{1100}C \next{1101}D \next{1110}E \next{1111}F
304 % \end{macrocode}
305 % \end{macro}
306 % \end{macro}
307 % \end{macro}
308 % \end{macro}
309 % \end{macro}
310 %
311 % \begin{macro}{\bb@dohex}
312 % |\bb@dohex| is the magic macro that initiates accumulation of enough
313 % binary digits for one hexadecimal one. It is called in
314 % the following form:
315 % \begin{quote}
316 % |\bb@dohex| \meta{m\{\#rhex\}} |\endcsname| \meta{m\{\#bin\}} |+|
317 % \end{quote}
318 % Where |+| is the tack after which hex digits will accumulate,
319 % \meta{m\{\#rhex\}} is a sequence of |m|, one for every remaining hex
320 % digit we want to force out (same convention as with |\nbinary| above),
321 % and \meta{m\{\#bin\}} is a sequence of |m| corresponding in length to
322 % the number of binary digits we need to accumulate for one hexadecimal
323 % (or octal) digit.
324 % \begin{macrocode}
325 \def\bb@dohex #1{\csname bb@x#1\endcsname}
326 % \end{macrocode}
327 %
328 % Eeek! What is that? Ok, let's split it into the two cases, one with
329 % forced digits remaining, one without any remaining forced digits.
330 % If no forced digits remain, we get
331 % \begin{quote}
332 % |\bb@x \endcsname \endcsname| \meta{m\{\#bin\}}
333 % \end{quote}
334 % \end{macro}
335 %
336 % \begin{macro}{\bb@x}
337 % |\bb@x| is defined as
338 % \begin{macrocode}
339 \def\bb@x\endcsname#1{ \bb@xm{m\endcsname}}
340 % \end{macrocode}
341 %
342 % Notice the space at the start of the macro! It will stop
343 % expansion. Furthermore, the |\endcsname| that |\bb@dohex| inserted is
344 % scrapped, as well as the first |m| from
345 % \meta{m\{\#bin\}}.\footnote{Why do we match |\string\endcsname| explicitly
346 % for scrapping? The answer is debugging. We know it should always
347 % be |\string\endcsname|, so let \TeX\ assert that it is indeed so. But we
348 % also know that the next character will be |m|, why don't we match
349 % \emph{that} explicitly? The answer is that we cannot be sure about the
350 % |\string\catcode| of the matched~|m|. A hand-entered |m| has
351 % |\string\catcode|~11, a \TeX-generated one (with |\string\string| or
352 % |\string\romannumeral| or such) has |\string\catcode|~12. You'll
353 % find that all the code here has been carefully
354 % designed so that it will not care which it gets, so we don't make
355 % an exception here.
356 % }
357 % \end{macro}
358 %
359 % \begin{macro}{\bb@xm}
360 % The argument fed to |\bb@xm| is part of some jiggery-pokery we want
361 % to happen when |\bb@dobinary| resumes expansion. First, however, it
362 % will take a look and decide whether it will stop generating digits
363 % altogether. As |\string \bb@xm| does not start with |m|, |\bb@dobinary|
364 % will stop expansion when the decimal to convert has shrunk to 0.
365 % Fine. Now what does |\bb@xm| do?
366 % \begin{macrocode}
367 \def\bb@xm #1\endcsname #2#3+{#2#3%
368 \csname bb@h\number+\endcsname
369 #1\endcsname m#3+}
370 % \end{macrocode}
371 % \end{macro}
372 %
373 % This is rather straightforward for the case of forced digits: the
374 % number of binary digits is encoded in the form of \meta{m\{\#bin\}} in
375 % |#2#3|. After the conversion of those digits, the appropriate hex
376 % digit macro is called and sets up |\bb@dohex| again for the next hex
377 % digit. Piece of cake. Now what happens in the case we have resumed
378 % from |\do@binary| having had its option of stopping expansion?
379 %
380 % In that case, we get called with the next \meta{digit}
381 % already arriving. This looks more or less like
382 % \begin{quote}
383 % |\csname bb@|\meta{digit}|\bb@xm \endcsname{m\endcsname}|
384 % \meta{m\{\#bin-1\}}
385 % \end{quote}
386 % See what happens? This expands |\bb@xm|, after which a command of the
387 % name |\bb@|\meta{digit}|m| (\meta{digit} being 0 or~1) gets executed.
388 % It stashes away \meta{digit} after
389 % the conveniently provided |+| sign, and \meta{\#bin-1} digits remain for
390 % conversion.
391 %
392 % You think this contrived? Well, buster, let me tell you that the
393 % previous version was way more insane. Have it still in RCS.
394 %
395 % \begin{macro}{\bb@nbinbased}
396 % The following macro needs to get |#1|, the decimal number to convert,
397 % in text form already. This is so that |\number#1| will continue
398 % expanding after |#1|, expanding |\bb@dohex| exactly once. This problem
399 % does not exist for the |\romannumeral| calls for |#2| and |#3|, so
400 % spurious expansion with |\number\number| does no harm, since it is
401 % stopped readily at the hard |\endcsname| and |+| thresholds.
402 % \begin{macrocode}
403 \def\bb@nbinbased #1#2#3{\expandafter \bb@dobinary \number#1%
404 \expandafter \bb@dohex
405 \romannumeral \number\number #2 000\expandafter\endcsname
406 \romannumeral \number\number #3 000+}
407 % \end{macrocode}
408 % \end{macro}
409 %
410 % \begin{macro}{\nbinbased}
411 % The following macro gets 3~arguments, the number of binary digits the
412 % output digits shall be based on, the number of digits to produce at
413 % least, and the number itself. It expands the number into text
414 % form. If the number is not space-terminated, the closing brace
415 % reliably stops expansion nevertheless, so that |\bb@nbinbased| gets a
416 % clean number.
417 % \begin{macrocode}
418 \def\nbinbased #1#2#3{\expandafter\bb@nbinbased
419 \expandafter {\number#3}{#2}{#1}}
420 % \end{macrocode}
421 % \end{macro}
422 % \begin{macro}{\nhex}
423 % \begin{macro}{\noct}
424 % \begin{macro}{\ntetra}
425 % \begin{macro}{\hex}
426 % \begin{macro}{\oct}
427 % \begin{macro}{\tetra}
428 % Now follow the obvious definitions:
429 % \begin{macrocode}
430 \def\nhex{\nbinbased4}
431 \def\noct{\nbinbased3}
432 \def\ntetra{\nbinbased2}
433 \def\hex{\nhex1}
434 \def\oct{\noct1}
435 \def\tetra{\ntetra1}
436 % \end{macrocode}
437 % \end{macro}
438 % \end{macro}
439 % \end{macro}
440 % \end{macro}
441 % \end{macro}
442 % \end{macro}
443 % \subsection{The end}
444 % Restore |\catcode`\@| and |\toks0| and finish.
445 % \begin{macrocode}
446 \the\toks0
447 %</style>
448 % \end{macrocode}
449 % And that was that!
450 % \section{Various drivers}
451 % The installer, in case it is missing.
452 % \begin{macrocode}
453 %<installer> \input docstrip
454 %<installer> \generate{
455 %<installer> \file{binhex.drv}{\from{binhex.dtx}{driver}}
456 %<installer> \usedir{tex/generic/misc}
457 %<installer> \file{binhex.tex}{\from{binhex.dtx}{style}}
458 %<installer> }
459 %<installer> \endbatchfile
460 % \end{macrocode}
461 % And here comes the documentation driver.
462 % \begin{macrocode}
463 %<driver> \documentclass{ltxdoc}
464 %<driver> \usepackage{amsmath}
465 %<driver> \input binhex.tex
466 % \end{macrocode}
467 % \begin{macro}{\verbatize}
468 % |\detokenize| is available in e\TeX, but we may not have that\dots
469 % The |\fontdimen| folderol makes spaces temporarily disabled. Looks
470 % prettier. Sorry for that.
471 % \begin{macrocode}
472 %<driver> \def\verbatize#1{\begingroup
473 %<driver> \toks0{#1}\edef\next{\the\toks0}%
474 %<driver> \dimen0\the\fontdimen2\font
475 %<driver> \fontdimen2\font=0pt
476 %<driver> \expandafter\stripit
477 %<driver> \meaning\next
478 %<driver> \fontdimen2\font=\dimen0
479 %<driver> \endgroup}
480 %<driver> \def\next{}
481 %<driver> \expandafter\def\expandafter\stripit\meaning\next{}
482 % \end{macrocode}
483 % \end{macro}
484 % \begin{macro}{\showeffect}
485 % |\showeffect|\meta{Stuff} will put out a one-line correspondence of
486 % the verbatim source of \meta{Stuff} with its expansion, to be used
487 % in a |aligned| environment or similar.
488 % \begin{macrocode}
489 %<driver> \newcommand\showeffect[1]{%
490 %<driver> \text{\verbatize{#1}}\quad&\rightarrow\quad\text{#1}%
491 %<driver> \\}
492 % \end{macrocode}
493 % \end{macro}
494 % \begin{macro}{\samples}
495 % |\samples| gets one parameter, and a following expression list ended
496 % by a period. It applies that parameter to each of the expressions
497 % in the list and generates a correspondance table for each. For
498 % example,
499 % \begin{verbatim}
500 %\samples{\nbinary{3}}{3}{-2}{-12}.
501 % \end{verbatim}
502 % was used in this document to produce
503 % \samples{\nbinary{3}}{3}{-2}{-12}.
504 % \begin{macrocode}
505 %<driver> \def\samples{\begingroup\MacroFont\[\begin{aligned}\nextsample}
506 %<driver> \def\nextsample#1#2{%
507 %<driver> \if.\noexpand#2\expandafter\endsamples\fi
508 %<driver> \showeffect{#1{#2}}\nextsample{#1}}
509 %<driver> \def\endsamples#1\nextsample#2{\end{aligned}\]\endgroup}
510 % \end{macrocode}
511 % \end{macro}
512 % Now all that remains is inputting the stuff.
513 % \begin{macrocode}
514 %<driver> \begin{document}
515 %<driver> \DocInput{binhex.dtx}
516 %<driver> \end{document}
517 % \end{macrocode}
518 % \Finale{}