5
$\begingroup$

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.

enter image description here

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.

$\endgroup$
  • 7
    $\begingroup$ $PrePrint = HoldForm[#] /. k_Integer :> BaseForm[k, 16] & ? $\endgroup$ – Szabolcs Oct 6 '14 at 22:56
  • $\begingroup$ For input try 16^^digits or 16^^digits.digits For example, 16^^ff will input 255, and 16^^ff.8 will input 255.5 $\endgroup$ – m_goldberg Oct 6 '14 at 23:29
  • $\begingroup$ @Szabolcs. Better to leave off the _Integer, No? Then it will work for inexact hex numbers too. $\endgroup$ – m_goldberg Oct 6 '14 at 23:36
  • 1
    $\begingroup$ @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. $\endgroup$ – William Oct 7 '14 at 14:48
  • 1
    $\begingroup$ Related mathematica.stackexchange.com/questions/30884/… $\endgroup$ – William Oct 8 '14 at 3:19
1
$\begingroup$

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_] /; format`hex =!= True := 
 Block[{format`hex = True}, ToBoxes[foo, form] /. s_String?DigitQ :>
   With[{n = FromDigits@s},
     InterpretationBox[StyleBox[#, RGBColor[1, 0, 0]], n] &[
       "\"" <> IntegerString[n, 16] <> "\""
     ]
   ]
 ]

Now:

enter image description here

$\endgroup$
0
$\begingroup$

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}
    ];
$\endgroup$
  • $\begingroup$ Non uppercase version "0123456789abcdef", "oabcdefghi" $\endgroup$ – William Oct 14 '14 at 21:23
  • $\begingroup$ "OabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ',<>{}-_/?\|" $\endgroup$ – William Oct 15 '14 at 23:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.