How can I convert for example
62343 to a hexadecimal value
45 to a hexadecimal value
?
I see I can use:
IntegerDigits[62343, 16] = {15, 3, 8, 7}
IntegerDigits[45, 16] = {2, 13}
I would like to have:
F387
2D
Try this
IntegerString[62343, 16] // ToUpperCase
#[[1]] <> If[Length[#] > 5, ToString[("\[Times]16")^#[[4]], StandardForm], ""] &@StringCases[ ToString[BaseForm[62343.56, 16], StandardForm], (WordCharacter ... ~~ "." ~~ WordCharacter ...) | (DigitCharacter ..)]
$\endgroup$
Commented
Jan 21, 2020 at 2:10
a more elegent way: BaseForm[2.45, 16]
gives $2.7333_{16}$
use this answer can easily solve it: https://mathematica.stackexchange.com/a/213246/68689
A not-so-functional-programming style answer.
rule = <|10 -> "A", 11 -> "B", 12 -> "C", 13 -> "D", 14 -> "E",
15 -> "F"|>;
f[x_] := Block[{},
frac = FractionalPart[x];
get = {};
Do[AppendTo[get, IntegerPart[16*frac]];
frac = FractionalPart[16 frac];
, 10];
get /. rule // ToString /@ # & // StringJoin // Return[
ToUpperCase@IntegerString[IntegerPart[x], 16] <> "." <> #] &
]
f[2.45] gives
"2.7333333333"
For integer input, you can use
System`Convert`CommonDump`ConvertBase[#, 16] & /@ {62343, 45}
{"F387", "2D"}
System`Convert`CommonDump`ConvertBase[#, 16] & /@ {62343.5, 45}
$\endgroup$
Commented
Jan 21, 2020 at 6:23
OK, I think I have a sense of what you want. Use the fact that the split string from BaseForm has either two or four elements, and those are already strings.
baseHelper[{number_, _}] := number
baseHelper[{exponent_, mantissa_, base_, _}] := mantissa <> "\[Times]" <>
base <> "^" <> exponent
baseString[number_, base_] := baseHelper[BaseForm[number, base] //
ToString // StringSplit]
Works for integers:
baseString[257, 16]
(* 101 *)
Small floating point:
baseString[176.23, 16]
(* b0.3ae *)
Big floating point:
baseString[10045.21^6, 16]
(* "d.9915×16^19" *)