Hexadecimal line numbers in listings

One could do this with no package and little effort using TeX arithmetic so it is bit overfill to use xintbinhex here but it works. But it does not provide macros to pad to a given number of hex-digits, I needed to code it here.

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{listings}

\lstset{basicstyle={\ttfamily\small}}
\lstset{keywordstyle={\bfseries}}
\lstset{numbers=left, numberstyle=\ttfamily\tiny}

\usepackage{xintbinhex}

\makeatletter
\newcommand\myHexNumber[1]{\ttfamily\tiny
                           \romannumeral\expandafter\myHexNumber@pad
                           \romannumeral0\xintdectohex{#1}.}%
% change 2 into how many hex digits are asked for with leading 0s
% (I guess 2 or 3 ...)
\def\myHexNumber@pad#1.{\xintreplicate{2-\xintLength{#1}}{0}#1}
\makeatother
\begin{document}


\begin{lstlisting}[language={[x86masm]Assembler}, numberstyle=\myHexNumber]
mov ax, [10]
jz 0A
add bx, [11]
dec ax
hjmp 02
mov [12], bx
hlt
\end{lstlisting}

\end{document}

This picture does not really prove it does work... but it does, no doubt about it! But it was done when I erroneously used \sffamily. Now corrected to \ttfamily see next image.

enter image description here

enter image description here


Here is a solution using packages xintbinhex for hexadecimal numbers, calc for counting by steps of 2, and renewing the \thelstnumber command for numbering, as indicated in the listings documentation:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{listings}
\usepackage{xintbinhex}
\usepackage{calc}

\lstset{basicstyle={\ttfamily\small}}
\lstset{keywordstyle={\bfseries}}
\lstset{numbers=left, numberstyle=\sffamily\tiny}
\renewcommand*\thelstnumber{\ifnum\value{lstnumber}<8 0\fi \xintDecToHex{\the\value{lstnumber}*2}}

\begin{document}

\begin{lstlisting}[language={[x86masm]Assembler},firstnumber=0]
mov ax, [10]
jz 0A
add bx, [11]
dec ax
jmp 02
mov [12], bx
hlt
\end{lstlisting}

\end{document}

enter image description here