解碼 XeLaTeX 中的十六進位編碼行

解碼 XeLaTeX 中的十六進位編碼行

我將嘗試適應pax.styXeLaTeX 並發現.pax檔案中的 url 被編碼為十六進位行:

\[{annot}{1}{Link}{56.693 693.9 156.107 707.7}{URI}{
  URI={\<6D61696C746F3A68656C6C6F406578616D706C652E636F6D\>},
  Border={[0 0 0]},
}\\

解碼後的URI是(可以透過 看到)mailto:[email protected]xxd -r -p

如何解碼 XeLaTeX 中的十六進位行(十六進位到 ascii)?

微量元素:

\documentclass{article}
\newcommand{\hextotext}[1]{#1} % ???
\begin{document}
\hextotext{6D61696C746F3A68656C6C6F406578616D706C652E636F6D}
\end{document}

答案1

您可以使用\str_set_convert:Nnnn從(假設)轉換utf8/hexutf8

\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}

這列印:

在此輸入影像描述

答案2

在特殊情況下「十六進位ASCII碼「(如您的問題中所述)您可以,例如,小寫整個序列,始終^^在該序列的兩個連續字符之前插入,並且您有一些可以解釋的內容,例如,作為- 表示法\scantokens中的相應字符序列^^

\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}

在此輸入影像描述

但這只是為了玩玩。

在現實生活場景中我會使用Phelype Oleinik 的回答中提供了什麼。這涵蓋了 utf8/hex 到 utf8 的轉換,而 ASCII(我的答案中涵蓋的內容)只是 utf8 的子集。

相關內容