5

How do I write an inline "\0x" without having LaTeX put each value on a new line?

"\0x" in C programing identifies a hexadecimal value. I would like to write an inline string of hexadecimal represented values.

Consider this as what I want to write

\0x68 \0x6F \0x77 \0x20 \0x64 \0x6F \0x3F

I tried

\\0x68 \\0x6F \\0x77 \\0x20 \\0x64 \\0x6F \\0x3F

but every hexadecimal value is placed on a newline, and missing the backslash.

I also tried

\texttt{\0x68\0x6F\0x77\0x20\0x64\0x6F\0x3F}

but the same thing happens.

egreg
  • 1,121,712
j0h
  • 171
  • 1
  • 6

4 Answers4

7

enter image description here

\documentclass{article}
\newcommand\0[3]{\texttt{\string\0#1#2#3}}
\begin{document}

\0x68 \0x6F \0x77 \0x20 \0x64 \0x6F \0x3F

\end{document}

David Carlisle
  • 757,742
4

If you have many of these, it makes sense to define your own command, say \hexa

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\hexa}{sm} {% * means also compute the representation % #2 = number to represent \texttt { \IfBooleanTF { #1 } { \joh_hexa:e { \int_to_Hex:n { #2 } } } { \joh_hexa:n { #2 } } } }

\cs_new:Nn \joh_hexa:n { \char_generate:nn { `\ } { 12 } 0x #1 } \cs_generate_variant:Nn \joh_hexa:n { e }

\ExplSyntaxOff

\begin{document}

\hexa{68} \hexa{6F} \hexa{77} \hexa{20} \hexa{64} \hexa{6F} \hexa{3F}

\hexa*{123456}

\hexa*{\value{page}}

\end{document}

You see that with \hexa* TeX will do the conversion to hexadecimal for you and you can even use a LaTeX counter as shown.

enter image description here

Using a command is better because, if you later change your mind and, for instance, decide to not use the backslash, you can just change the definition in a single place.

egreg
  • 1,121,712
3

A solution with several instances of \string:

enter image description here

\documentclass{article}
\begin{document}
\texttt{\string\0x68\string\0x6F\string\0x77\string\0x20\string\0x64\string\0x6F\string\0x3F}
\end{document}
Mico
  • 506,678
2

Please try this:

\documentclass{article}

\begin{document}

The value \texttt{\textbackslash0x68} is a hexadecimal code.

\end{document}