2
$\begingroup$

I want to get the string "b0.3ae"

In:=BaseForm[176.23, 16]
Out:=Subscript[b0.3ae, 16]

And then I want to get 176.23 by FromDigits["b0.3ae", 16] operation.

But I can't do this very well.

We also need to consider the case of this scientific counting method:

BaseForm[10045.21^6, 16]
$\endgroup$
3
$\begingroup$
fromBaseForm = RawBoxes[ToBoxes[#] /. SubscriptBox[a_, _] :> a]&;

Examples:

fromBaseForm @ BaseForm[176.23,16]

"b0.3ae"

fromBaseForm @ BaseForm[10045.21^6,16]

"d.9915"*16^("19")

$\endgroup$
2
$\begingroup$

For your first case:

(BaseForm[176.23, 16] // ToString // StringSplit)[[1]]

yields

"b0.3ae"

which seems to be what you want.

It isn't clear what you want in the second case. A similar technique should work, though.

$\endgroup$
  • $\begingroup$ But this operation is not valid for (BaseForm[10045.21^6, 16] // ToString // StringSplit) $\endgroup$ – Please Correct GrammarMistakes Jan 21 at 1:28
  • $\begingroup$ Yes. StringSplit yields four strings for that case (decimal exponent, hexadecimal mantissa, and two copies of "16"). You can use them as you wish, but you haven't stated what output you want, so I can't write the code. $\endgroup$ – John Doty Jan 21 at 1:38
  • $\begingroup$ I want string results in the form of "d.9915" <> "\[Times]" <> ToString[Defer[16^19], FormatType -> StandardForm] $\endgroup$ – Please Correct GrammarMistakes Jan 21 at 1:51
1
$\begingroup$

I did it like this, but it's too complicated :

#[[1]] <> 
   If[Length[#] > 5, ToString[("\[Times]16")^#[[4]], StandardForm], 
    ""] &@StringCases[
  ToString[BaseForm[10045.21^6, 16], 
   StandardForm], (WordCharacter ... ~~ "." ~~ 
     WordCharacter ...) | (DigitCharacter ..)]
$\endgroup$

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.