[Tex/LaTex] Hex renderer in LaTeX

rendering

Is there a package to render binary files as hex in LaTeX. We've searched for it, but couldn't find any.

The package should provide some kind of command:

\lsthex{file.ext}

With file.ext a "binary" file (in this case the header of a .png file, but the content is here of course irrelevant).

Resulting in something like:

| 89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52 |
| 00 00 00 10 00 00 00 10 08 06 00 00 00 1F F3 FF |

Best Answer

Here is a version that works in MacOs (and should work on most Unix machines):

enter image description here

Notes:

  • You can replace the hexdump with different shell command. This method will show the output of that.
  • This requires the -shell-escape option to allow for executing shell commands.

Code:

\documentclass{article}
\usepackage{graphicx}
\usepackage{listings}

\newcommand{\ShowHexOutput}[2][\jobname.hex]{%
    % #1 = temp file name       
    % #2 = input file name 
    %
    \immediate\write18{hexdump -C #2 > ./#1}%
    \lstinputlisting{./#1}%
}%

\lstset{basicstyle=\tiny\ttfamily}


\begin{document}
The image
\includegraphics[width=0.5\linewidth]{../images/EiffelWide.jpg} 
in hex looks like:

\ShowHexOutput{../images/EiffelWide.jpg}
\end{document}
Related Question