Decoding hex-encoded line in XeLaTeX

You can use \str_set_convert:Nnnn to convert from (assuming) utf8/hex to utf8:

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\str_new:N \l__ivanov_tmpa_str
\NewDocumentCommand \hextotext { o m }
  {
    \str_set_convert:Nxnn \l__ivanov_tmpa_str {#2}
      { utf8/hex } { utf8 }
    \IfValueTF{#1}
      { \str_set_eq:NN #1 \l__ivanov_tmpa_str }
      { \str_use:N \l__ivanov_tmpa_str }
  }
\cs_generate_variant:Nn \str_set_convert:Nnnn { Nx }
\ExplSyntaxOff
\begin{document}

\hextotext{6D61696C746F3A68656C6C6F406578616D706C652E636F6D}

\hextotext[\tmp]{6D61696C746F3A68656C6C6F406578616D706C652E636F6D}
\texttt{\meaning\tmp}

\end{document}

This prints:

enter image description here


In the special case "hex to ascii" (as stated in your question) you can, e.g., lowercase the whole sequence, always insert ^^ before two successive characters of this sequence, and you have something that is interpreted, e.g., by \scantokens as the corresponding character sequence in ^^-notation.

\documentclass{article}

\begingroup
\makeatletter
\catcode`\^^A=14\relax
\catcode`\%=12\relax
\@firstofone{^^A
  \endgroup
  \newcommand\hextotext[2]{^^A
    \@ifdefinable#1{^^A
      \lowercase{^^A
        \scantokens\expandafter{^^A
          \expandafter\newcommand\expandafter#1\expandafter{^^A
            \romannumeral\expandafter\hextotextloop\expandafter{\expandafter}\detokenize{#2}\relax\relax
          }%^^A
        }^^A
      }^^A
    }^^A
  }^^A
}%
\begingroup
\makeatletter
\catcode`\^=12\relax
\@firstofone{%
  \endgroup
  \newcommand*\hextotextloop[3]{%
    \ifx\relax#2\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
    {\z@#1}{\hextotextloop{#1^^#2#3}}%
  }%
}%

\begin{document}

%  6D61696C746F3A68656C6C6F406578616D706C652E636F6D

^^6d^^61^^69^^6c^^74^^6f^^3a^^68^^65^^6c^^6c^^6f^^40^^65^^78^^61^^6d^^70^^6c^^65^^2e^^63^^6f^^6d

\hextotext{\MyCommand}{6D61696C746F3A68656C6C6F406578616D706C652E636F6D}

\begingroup

\ttfamily

\string\MyCommand: \meaning\MyCommand

\endgroup

\MyCommand

\end{document}

enter image description here

But this is for playing around only.

In real-life-scenarios I'd use what is provided in Phelype Oleinik's answer. That covers conversion of utf8/hex to utf8 while ASCII (what is covered in my answer) is just a subset of utf8.

Tags:

Macros

Xetex