Q:

Treat Macro Argument as Hex Number

I’m writing a macro to output Unicode codepoints in the U+1F642  SLIGHTLY SMILING FACE format. To do this, I need to print an argument as a hex number as well as pass it to \symbol.
Minimal non-working example:
\newcommand{\unicode}[3][]
    {\texttt{U+#1} {#3 \symbol{"#1}} \texttt{#2}}

I think some \expandafter or counter magic is required, but I’m not exactly sure where to go.
I can assume the codepoint will always be in upper-case hexadecimal, so multiple number-formats don’t need to be accounted for.
If it makes a difference, I’m using XeLaTeX.
The error is unsurprising:
! Missing number, treated as zero.
<to be read again>
                   {
\\unicode [#1]#2#3->\texttt {U+#1} \symbol {"#1}
                                                 \texttt {#2}

A:

\char is a TeX primitive that takes as argument a <number> and doesn't want braces as delimiters of the argument.
LaTeX provides \symbol as a wrapper around it.
\newcommand{\unicode}[2]{%
  \texttt{U+#1} \symbol{"#1} \texttt{#2}%
}

With \char it should be
\newcommand{\unicode}[2]{%
  \texttt{U+#1} \char"#1\space\space\texttt{#2}%
}

(the first \space is gobbled as the <number> terminator, the second one is typeset).
Minimal example:
\documentclass{article}
\usepackage{fontspec}
\setmainfont{CMU Serif}

\newcommand{\unicode}[2]{%
  \texttt{U+#1} \symbol{"#1} \texttt{#2}%
}

\begin{document}

\unicode{0180}{LATIN SMALL LETTER B WITH STROKE}

\unicode{04AF}{CYRILLIC SMALL LETTER STRAIGHT U}

\end{document}

However, if you want to be able to specify a different font for some character, the definition can be as follows. Note the order of the arguments.
\documentclass{article}
\usepackage{fontspec}
\setmainfont{CMU Serif}
\newfontfamily{\djvusans}{DejaVu Sans}

\newcommand{\unicode}[3][]{%
  \texttt{U+#2} {#1\symbol{"#2}} \texttt{#3}%
}

\begin{document}

\unicode{0180}{LATIN SMALL LETTER B WITH STROKE}

\unicode{04AF}{CYRILLIC SMALL LETTER STRAIGHT U}

\unicode[\djvusans]{1F0CA}{PLAYING CARD TEN OF DIAMONDS}

\end{document}

If you want the optional argument to be last (but I don't recommend it), you can use \NewDocumentCommand, provided by xparse which is loaded by fontspec.
\documentclass{article}
\usepackage{fontspec}
\setmainfont{CMU Serif}
\newfontfamily{\djvusans}{DejaVu Sans}

\NewDocumentCommand{\unicode}{mmO{}}{%
  \texttt{U+#1} {#3\symbol{"#1}} \texttt{#2}%
}

\begin{document}

\unicode{0180}{LATIN SMALL LETTER B WITH STROKE}

\unicode{04AF}{CYRILLIC SMALL LETTER STRAIGHT U}

\unicode{1F0CA}{PLAYING CARD TEN OF DIAMONDS}[\djvusans]

\end{document}

