[Tex/LaTex] Parsing hexadecimal numbers to binary and iterating over bits

package-writingparsingtikz-pgf

I'm currently working on a software for an graphic display, so I recreated it in TikZ for documentation purposes:

image of the display

I currently use this to set individual pixels on the display:

\setpixel{x}{y}

Where x and y are coordinates between 0 and 132/64.

The real display is separated into 8 pages, each 8 pixels high, and a byte sent to the display is displayed as a column. See this image for details.

Since I don't want to always calculate the individual pixels, I'd like the LaTeX variant to behave like the real display, that is, I want some command like

\dispbyte{0x01}
\dispbyte{0x03}
\dispbyte{0x07}
\dispbyte{0x0F}
\dispbyte{0x1F}
\dispbyte{0x3F}
\dispbyte{0x7F}
\dispbyte{0xFF}

(this would create one of the triangles above)
I have an idea how I'd implement counting of the current column and page switching and all that — shouldn't be hard with some counters after all — but I just can't find out how to parse the hexadecimal values and then iterate over every bit inside them.

I've found fmtcount and binhex.tex, but they only display a LaTeX counter in another format, and I can't understand their code at all.

The finished source code (display.sty) and some examples are now available at http://cmpl.cc/downloads/disp/

Best Answer

The following is less impressive than the other answers from the visual point of view (Mark, I like yours!), but addresses the actual question of the OP: Bit-wise iteration over hexadecimal values, which becomes fairly easy when using the bitset package by Heiko Oberdiek:

\documentclass{article}
\usepackage{bitset}
\usepackage{pgf,pgffor}

\begin{document}

  \bitsetSetHex{mybitset}{AA}
  % use \bitsetGetSetBitList
  % expand first
  \edef\mybits{\bitsetGetSetBitList{mybitset}}
  \noindent
  \foreach \bit in \mybits {%
    Bit \bit{} is set! \\ 
  }

  % just itereate all bits
  \noindent
  \foreach \i in {0,...,7} {%
    Bit \i: \bitsetGet{mybitset}{\i} \\
  }

\end{document}

enter image description here

Related Question