# Display All Output Numbers in HEX

How might I modify Mathematica such that I can get the following functionality when working with HEX values. The odd lines are input and the even output. Red values should be the HEX values.

What would be the most portable and functionally useful way to display all numbers in HEX? I would like to use either Notation, Symbolize, or Interpretation to display the numbers as HEX but allow them to be interpreted as actual numbers internally.

• \$PrePrint = HoldForm[#] /. k_Integer :> BaseForm[k, 16] & ? Oct 6, 2014 at 22:56
• For input try 16^^digits or 16^^digits.digits For example, 16^^ff will input 255, and 16^^ff.8 will input 255.5 Oct 6, 2014 at 23:29
• @Szabolcs. Better to leave off the _Integer, No? Then it will work for inexact hex numbers too. Oct 6, 2014 at 23:36
• @Szabolcs That works well for displaying simple examples, but I would really like to use Interpretation or Symbolize(or something similar) to effectively display the numbers in HEX while still keeping the values represented as Integers when trying to evaluate them. It gets frustrating not being able to edit output cells like familiar with. Oct 7, 2014 at 14:48
• Oct 8, 2014 at 3:19

Here is my first pass at implementing what you describe. If you find that it deviates from your intended behavior let me know and I shall attempt to refine it.

MakeBoxes[foo_, form_] /; formathex =!= True :=
Block[{formathex = True}, ToBoxes[foo, form] /. s_String?DigitQ :>
With[{n = FromDigits@s},
InterpretationBox[StyleBox[#, RGBColor[1, 0, 0]], n] &[
"\"" <> IntegerString[n, 16] <> "\""
]
]
]


Now:

Currently you must call d@Sum[x^100, {x, 0, 10}] to get it to display in HEX form. To input custom HEX numbers simply call use the 16^^ff. In addition I have included a custom base16 function because I prefer a generalized solution that works for bases larger then 36 and support capital letters in stead of lowercase letters.

t[x_] := Module[{l, r, h},
l = DeleteDuplicates@Select[
Cases[x, _String, {0, Infinity}],
StringMatchQ[#, DigitCharacter ..] &];
h = Replace[
l, {k_String :> {k, ToExpression@k, base16@ToExpression@k}}, {1}];
r = Replace[
h, {a_, b_,
c_} :> (a :>
InterpretationBox[
StyleBox[c, FontColor -> RGBColor[1, 0, 0],
StripOnInput -> False], b]),
{1}
];
Replace[x, r, {0, Infinity}]
];
SetAttributes[d, HoldFirst];
d /: MakeBoxes[d[x_], form_] :=
t[ToBoxes@Unevaluated@x];
base16[x_] := StringJoin@Replace[
IntegerDigits[x, 16],
MapIndexed[(
(#2[[1]] - 1) :> #
) &, StringSplit["0123456789ABCDEF", ""]]
, {0, Infinity}
];

• Non uppercase version "0123456789abcdef", "oabcdefghi" Oct 14, 2014 at 21:23
• "OabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ',<>{}-_/?\|" Oct 15, 2014 at 23:58