3
$\begingroup$

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]

enter image description here

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.

$\endgroup$
5
  • 1
    $\begingroup$ You can wrap you numbers in PaddedForm to get the leading zero $\endgroup$ Oct 30, 2013 at 23:38
  • $\begingroup$ 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. $\endgroup$ Oct 31, 2013 at 15:51
  • $\begingroup$ 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? $\endgroup$
    – ssch
    Oct 31, 2013 at 18:45
  • $\begingroup$ @ssch Yes! That is exactly right, it works. $\endgroup$ Oct 31, 2013 at 19:01
  • $\begingroup$ 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] $\endgroup$ Oct 31, 2013 at 21:59

4 Answers 4

4
$\begingroup$

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 &)]

enter image description here

$\endgroup$
1
  • $\begingroup$ PaddedForm pads with zeros: left or right or both. $\endgroup$ Oct 31, 2013 at 21:59
1
$\begingroup$

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:

enter image description here

$\endgroup$
0
$\begingroup$

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]

enter image description here

StringTake[ToString[b], 6]

a7d8c0
$\endgroup$
7
  • $\begingroup$ How does this help me for the expression shown in the question? $\endgroup$ Oct 31, 2013 at 15:47
  • $\begingroup$ It answers your second question: how do I remove the "16s". You need simply do this same thing to every entry in your table. $\endgroup$
    – bill s
    Oct 31, 2013 at 16:21
  • $\begingroup$ Show me the money. How would I modify the expression in the question to do this? $\endgroup$ Oct 31, 2013 at 16:28
  • $\begingroup$ 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. $\endgroup$
    – bill s
    Oct 31, 2013 at 18:05
  • $\begingroup$ Its not an answer. $\endgroup$ Oct 31, 2013 at 18:30
0
$\begingroup$

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

output


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]})
$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$ Oct 31, 2013 at 15:50
  • $\begingroup$ Yes, I believe Mathematica prefers base 10, at least in terms of the interface it offers to programmers. $\endgroup$
    – DavidC
    Oct 31, 2013 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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