1

How can I convert a user-supplied hexadecimal string like 14b or 14B into something which (La)TeX will recognise as a number? I suppose that a package like xcolor can do it, but I have not been able to find out how (Google is no help here). I'm aware of "XX but it only goes up to FF, or FFFF in XeTeX, and that's not enough in all cases for me. Converting to decimal during input is not really an option. Note that this is not about typesetting numbers in different forms but about converting a string argument representing a hex number into something which can be used in calculations.

4
  • 1
    If you are using a number in a calculation (\intexpr or similar) then simply using the " notation should be fine.
    – Joseph Wright
    Jul 27, 2017 at 12:45
  • I need to go up to at least five hex digits, which is out of range for " notation. I said so in the question although perhaps not clearly enough.
    – bpj
    Jul 27, 2017 at 12:50
  • 1
    No, it's not out-of-range: try for example \number\numexpr"ABCDE + 10\relax
    – Joseph Wright
    Jul 27, 2017 at 12:59
  • OK now I can do addition/ subtraction with hex numbers with five digits, but here comes the next problem: use the result as an argument to LuaLaTeX \Uchar among other things doesn't seem to work. I'll make a separate question of that.
    – bpj
    Jul 27, 2017 at 13:44

2 Answers 2

2

The \int_from_hex:n function of expl3 accepts both uppercase and lowercase letters for the digits from A to F. So you can do

\usepackage{expl3}

\ExplSyntaxOn
\cs_set_eq:NN \intfromhex \int_from_hex:n
\ExplSyntaxOff

and \Uchar\intfromhex{14b} or \Uchar\intfromhex{14B} will produce the same result.

Actually expl3 has its own version of \Uchar, but it's another topic.

If you need to do arithmetic, add also

\cs_set_eq:NN \inteval \int_eval:n

before \ExplSyntaxOff and something like

\Uchar\inteval{36+\intfromhex{14b}}

would work (here 36 is in decimal).

Note that 36+"14b would not work, because only uppercase letters are allowed in the " notation. Usage of \intfromhex converts to decimal.

1

\usepackage{xintbinhex}, then \xintDecToHex{\the\numexpr "ABCDE + "EBCDA\relax} expands to the result in hexadecimal notation but without the leading ". But in any context where TeX expects a number you will be able to use

"\xintDecToHex{\the\numexpr "ABCDE + "EBCDA\relax}

syntax, with a manually added " upfront.

In case the result of the \numexpr would exceed the TeX bound (which in hexadecimal is "7FFFFFFF), you can load package xintexpr. Then

\xintDecToHex{\thexintiiexpr "7FFFFFFF + "1\relax}

will work and produce 80000000 as expected. Again without the leading ".

\documentclass{scrartcl}

\usepackage{xintbinhex}
\usepackage{xintexpr}
\begin{document}

\xintDecToHex{\the\numexpr "ABCDE + "EBCDA\relax}

\xintDecToHex{\the\numexpr "111111 + "ABCDEF\relax}

\count255 = "\xintDecToHex{\the\numexpr "ABCDE + "EBCDA\relax}

\the\count255

\xintDecToHex{\thexintiiexpr "7FFFFFFF + "1\relax}

\end{document}

enter image description here

If your query is about converting hexadecimal to decimal, there is \xintHexToDec (what else ?)

\xintHexToDec{AAAAABBBBBCCCCCDDDDDEEEEEFFFFF99999}

Note that in this macro usage, no " prefix. Or, equivalently

\thexintiiexpr "AAAAABBBBBCCCCCDDDDDEEEEEFFFFF99999\relax

Both give:

enter image description here

Both require uppercase letters only, like TeX, but without the size limit.

1
  • of course the example with "\xintDecToHex{...} is only for demonstration, because the decimal number should be fetchable directly and not need to be converted to hexadecimal first just for the pleasure of the " prefix ...
    – user4686
    Jul 27, 2017 at 17:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .