4
$\begingroup$

How can I convert for example

  1. 62343 to a hexadecimal value

  2. 45 to a hexadecimal value

?

I see I can use:

  1. IntegerDigits[62343, 16] = {15, 3, 8, 7}

  2. IntegerDigits[45, 16] = {2, 13}

I would like to have:

  1. F387

  2. 2D

$\endgroup$

5 Answers 5

9
$\begingroup$

Try this

IntegerString[62343, 16] // ToUpperCase
$\endgroup$
5
  • $\begingroup$ But this operation is not valid for real number cases, such as 62343.56. $\endgroup$ Commented Jan 21, 2020 at 1:23
  • $\begingroup$ @Gowiththewind, I'm trying to understand how you'd expect a real number to be represented in hex. What would you expect for such a case? Hex representation of the floating point representation? Some assumed fractional representation and if so, how many bits? You could use IntegerString[NumeratorDenominator[Rationalize[62343.56,0]],16] yielding {"17c83d", "19"} $\endgroup$
    – Mark R
    Commented Jan 21, 2020 at 2:02
  • $\begingroup$ Or Divide @@ IntegerString[NumeratorDenominator[Rationalize[62343.56, 0]], 16], which shows/prints correctly (but not obvious that base 16, depending on numbers). $\endgroup$
    – Mark R
    Commented Jan 21, 2020 at 2:08
  • 1
    $\begingroup$ I did it like this, but it's too complicated : #[[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
  • $\begingroup$ To convert a fractional part to a different base, you do it by continually multiplying the fractional part by the base, then shifting the result and shifting the decimal point in the input. 10.5 in decimal becomes A.8 because 0.5 of 16 is 8 (8 in hex) and there's no further remainder. 10.1 in decimal becomes A.1999999... 10.8 in decimal becomes 10.CCCCCC... $\endgroup$
    – trr
    Commented Jan 21, 2020 at 3:14
5
$\begingroup$

If all you want is a base 16 display,

BaseForm[62343, 16]

yielding

enter image description here

$\endgroup$
3
$\begingroup$

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"

$\endgroup$
2
$\begingroup$

For integer input, you can use

System`Convert`CommonDump`ConvertBase[#, 16] & /@ {62343, 45}

{"F387", "2D"}

$\endgroup$
4
  • $\begingroup$ I'm curious where you get so many built-in functions which are not in the help documentation. $\endgroup$ Commented Jan 21, 2020 at 6:21
  • $\begingroup$ But it doesn't work: System`Convert`CommonDump`ConvertBase[#, 16] & /@ {62343.5, 45} $\endgroup$ Commented Jan 21, 2020 at 6:23
  • $\begingroup$ @Gowiththewind, updated with the caveat. $\endgroup$
    – kglr
    Commented Jan 21, 2020 at 6:52
  • $\begingroup$ Thank you,I just saw that the precondition is integers and I want to know if there is such a built-in function for real numbers. $\endgroup$ Commented Jan 21, 2020 at 7:04
2
$\begingroup$

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" *)
$\endgroup$

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.