# Padding and formatting within BaseForm

I find using BaseForm to be a little tricky. For example, if you use BaseForm and then do some additional operation all the numbers turn back into base 10, so you have do BaseForm as the "last step". This creates the problem of how you then format the BaseForm if it is the last step.

For example, given the following:

BaseForm[Grid[Partition[Map[FromDigits[#,16]&,RealDigits[FractionalPart[CubeRoot[Table[Prime[n],{n,64}]]],16,8,-1][[All,1]]],8],Alignment->Right],16]


Formatting problems are:

(1) how to get leading zeroes for those values that are less than 8 hex digits?

(2) how to get rid of the 16's (I don't want to display them)

Please do not answer with copy and paste from the doc center, unless you can you show how the command in question works in the above expression inside of BaseForm. PaddedForm and other similar commands DO NOT WORK as far as I can tell. For example if you write:

BaseForm[Grid[Partition[Map[PaddedForm[FromDigits[#,16],8]&,... etc


It does NOT work.

-
You can wrap you numbers in PaddedForm to get the leading zero –  Mike Honeychurch Oct 30 '13 at 23:38
I don't see how PaddedForm helps me here. Show me where in the expression from the question you can insert PaddedForm to add the leading zeroes. –  Tyler Durden Oct 31 '13 at 15:51
Since it's the last step, how about getting rid of BaseForm and putting something like /. {n_Integer :> IntegerString[n, 16, 8]} at the end? –  ssch Oct 31 '13 at 18:45
@ssch Yes! That is exactly right, it works. –  Tyler Durden Oct 31 '13 at 19:01
PaddedForm does give you padding on the right. Given the name shouldn't we expect that to be the case? I didn;t say you could remove the zeros that way -- was referring solely to the padding part of your question: BaseForm[PaddedForm[#, 8, NumberPadding -> {"0", ""}] & /@ Range[0, 15], 16] –  Mike Honeychurch Oct 31 '13 at 21:59

Incedentally it is NumberForm, not PaddedForm you want to right pad with zeros:

This works, though you are left with the 16 subscripts..

NumberForm[BaseForm[Grid[Partition[Map[FromDigits[#, 16] &,
RealDigits[FractionalPart[CubeRoot[Table[Prime[n], {n, 64}]]],
16, 8, -1][[All, 1]]], 8],
Alignment -> Right], 16], 8, NumberPadding -> "0",
NumberSigns -> {"", ""}]


Omitting that NumberSigns argument does odd things here..

Aha.. here you go.. use NumberFormat to strip the subscript..

NumberForm[BaseForm[Grid[Partition[Map[FromDigits[#, 16] &,
RealDigits[FractionalPart[CubeRoot[Table[Prime[n], {n, 64}]]],
16, 8, -1][[All, 1]]], 8],
Alignment -> Right], 16], 8, NumberPadding -> "0",
NumberSigns -> {"", ""}, NumberFormat -> (#1 &)]


-
PaddedForm pads with zeros: left or right or both. –  Mike Honeychurch Oct 31 '13 at 21:59

This answer is due to ssch. Instead of using BaseForm, use of IntegerString generates the correct output:

Grid[Partition[
Map[IntegerString[FromDigits[#, 16], 16, 8] &,
RealDigits[FractionalPart[CubeRoot[Table[Prime[n], {n, 64}]]], 16,
8, -1][[All, 1]]], 8], Alignment -> Right]


results in:

-

Here's one way to strip away the 16s by turning the BaseForm expression into a string and then removing all but the first several terms of the string.

b=BaseForm[11000000, 16]


StringTake[ToString[b], 6]

a7d8c0

-
How does this help me for the expression shown in the question? –  Tyler Durden Oct 31 '13 at 15:47
It answers your second question: how do I remove the "16s". You need simply do this same thing to every entry in your table. –  bill s Oct 31 '13 at 16:21
Show me the money. How would I modify the expression in the question to do this? –  Tyler Durden Oct 31 '13 at 16:28
Sorry, Tyler, that's not how it works. Let me see how you have tried to incorporate my suggestion. Edit your problem to add your attempts, and we'll see where you get stuck. –  bill s Oct 31 '13 at 18:05
Its not an answer. –  Tyler Durden Oct 31 '13 at 18:30

Here's a try, but there must be a shorter way. m is the main part of your above computations

m = Map[FromDigits[#, 16] &,  RealDigits[FractionalPart[CubeRoot[Table[Prime[n], {n, 64}]]], 16,  8, -1][[All, 1]]]


This is not elegant but it works.

StringJoin[ToString /@ PadLeft[Characters@StringDrop[ToString@BaseForm[#, 16], -11], 8]] & /@ m


And this is even less elegant...

IntegerDigits and PadLeft gets the characters you want. StringJoin joins the characters into a single string.

StringJoin @@@ (((PadLeft[IntegerDigits[#, 16], 8]) & /@ m) /. {10 -> "a", 11 -> "b",
12 -> "c", 13 -> "d", 14 -> "e", 15 -> "f", n_Integer :> ToString[n]})

-
Yes, I suppose we could go the route of first converting all the base forms to strings, manually padding them and manually stripping off the 16s from the strings, then putting the strings into a grid, but, as you say, this is very ugly. Are we running into a fundamental limitation here of Mathematica not being able to handle numbers other than base 10? –  Tyler Durden Oct 31 '13 at 15:50
Yes, I believe Mathematica prefers base 10, at least in terms of the interface it offers to programmers. –  David Carraher Oct 31 '13 at 16:41