RE: List in base 16
- To: mathgroup at smc.vnet.net
- Subject: [mg40174] RE: List in base 16
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Tue, 25 Mar 2003 03:03:05 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message----- >From: Steve Gray [mailto:stevebg at adelphia.net] To: mathgroup at smc.vnet.net >Sent: Monday, March 24, 2003 10:29 AM >To: mathgroup at smc.vnet.net >Subject: [mg40174] List in base 16 > > > BaseForm uses lower case letters and prints the subscript 16 >after each number. Is there a way to print a list whose elements are >integers 0 through 15 using 0,1,...,9,A,B,C,D,E,F without the >subscript 16? E.g. {8,13,11,3} should appear as {8,D,B,3}. > Help will be appreciated. Thank you. > > Steve, perhaps you can use this: In[2]:= rule16 = {0 -> "0", 1 -> "1", 2 -> "2", 3 -> "3", 4 -> "4", 5 -> "5", 6 -> "6", 7 -> "7", 8 -> "8", 9 -> "9", 10 -> "A", 11 -> "B", 12 -> "C", 13 -> "D", 14 -> "E", 15 -> "F"}; In[3]:= Attributes[HF] = Listable In[4]:= HF[n_Integer] := InterpretationBox[RowBox[#1], #2] &[IntegerDigits[n, 16] /. rule16, n] In[5]:= HF[expr_] := MakeBoxes[expr, StandardForm] In[6]:= HexForm[expr_] := HF[expr] // DisplayForm An integer now displays in the format you liked: In[7]:= 555 // HexForm Out[7]//DisplayForm= 22B Also integers nested in lists: In[8]:= {333, {444}, "hello"} // HexForm Out[8]//DisplayForm= {14D, {1BC}, "\"hello\""} You may return to the original expressions: In[9]:= %% // ToExpression Out[9]= 555 In[10]:= %% // ToExpression Out[10]= {333, {444}, "hello"} In a similar fashion, you also may deal with reals (then using RealDigits) In[11]:= HF[r_Real] := InterpretationBox[#1, #2] &[ StringJoin[{Take[#1, #2], ".", Drop[#1, #2]} & @@ RealDigits[r, 16] /. rule16], r] In[12]:= {705, 705.4926} // HexForm Out[12]//DisplayForm= {2C1, 2C1.7E1B089A02} In[13]:= Subtract @@ ToExpression[%] Out[13]= -0.4926 -- Hartmut Wolf