\newcount\hextemp
\newcount\hextempA
\newcount\hextempB
\newtoks\tokA
\def\hex#1{%
  \hextemp=#1\tokA{}%
  \ifnum#1<0\hextemp=-\hextemp\fi
  \hexloop
  \ifnum#1<0
    \expandafter\expandafter\expandafter\expandafter
    \expandafter\expandafter\expandafter\tokA
    \expandafter\expandafter\expandafter{\expandafter-\the\tokA}\fi
}
\def\hexloop{%
  \ifnum\hextemp>15\let\next\hexloop\else\let\next\relax\fi
  \hextempA=\hextemp\divide\hextemp16
  \hextempB\hextemp\multiply\hextempB16
  \advance\hextempA-\hextempB
  \edef\tempA{%
    \ifcase\hextempA 0\or1\or2\or3\or4\or5\or6\or7\or8\or9\or
    A\or B\or C\or D\or E\or F\fi\the\tokA}
  \expandafter\expandafter\expandafter\tokA\expandafter{\tempA}
  \next}
\def\out#1{\hex{#1}\message{\the\tokA}}
\scrollmode

\message{Test 1: }
\mathcode100="6ABC  \out{\mathcode100}
\delcode100="123456 \out{\delcode100}

\expandafter\ifx\csname omathcode\endcsname\relax
\else
\message{Test 2: }
\omathcode100="1020034 \out{\mathcode100} 
\odelcode100="010023 "040056\out{\delcode100}
\message{Test 3: }

\omathcode100="1234567 \out{\omathcode100}
\odelcode100="010023 "040056 \out{\odelcode100}
\odelcode100="123456 "ABCDEF\out{\odelcode100}
\fi
\end

