5

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.

William
  • 7,595
  • 2
  • 22
  • 70
  • 7
    $PrePrint = HoldForm[#] /. k_Integer :> BaseForm[k, 16] & ? – Szabolcs Oct 06 '14 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 – m_goldberg Oct 06 '14 at 23:29
  • @Szabolcs. Better to leave off the _Integer, No? Then it will work for inexact hex numbers too. – m_goldberg Oct 06 '14 at 23:36
  • 1
    @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. – William Oct 07 '14 at 14:48
  • @LiamWilliam I agree that something better is much preferred. I'm just commenting once again to correct my first comment to the much simpler $PrePrint = BaseForm[#, 16] &. This still doesn't do what you're asking for though. – Szabolcs Oct 07 '14 at 15:37
  • @Thanks that helps with a quick import form. Surely you mean something like (k_Integer | k_Rational | k_Real) because k_ is way to general. – William Oct 07 '14 at 15:51
  • 1
    Related http://mathematica.stackexchange.com/questions/30884/displaying-index-as-subscript-on-output-e-g-ci-c-i-with-notation-or – William Oct 08 '14 at 03:19

2 Answers2

1

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

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
0

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}
    ];
William
  • 7,595
  • 2
  • 22
  • 70