# Convert decimal value to hexadecimal

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

## 5 Answers

Try this

IntegerString[62343, 16] // ToUpperCase

• But this operation is not valid for real number cases, such as 62343.56. Jan 21, 2020 at 1:23
• @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"} Jan 21, 2020 at 2:02
• Or Divide @@ IntegerString[NumeratorDenominator[Rationalize[62343.56, 0]], 16], which shows/prints correctly (but not obvious that base 16, depending on numbers). Jan 21, 2020 at 2:08
• 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 ..)] Jan 21, 2020 at 2:10
• 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...
– trr
Jan 21, 2020 at 3:14

If all you want is a base 16 display,

BaseForm[62343, 16]


yielding

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

SystemConvertCommonDumpConvertBase[#, 16] & /@ {62343, 45}


{"F387", "2D"}

• I'm curious where you get so many built-in functions which are not in the help documentation. Jan 21, 2020 at 6:21
• But it doesn't work: SystemConvertCommonDumpConvertBase[#, 16] & /@ {62343.5, 45} Jan 21, 2020 at 6:23
• @Gowiththewind, updated with the caveat.
– kglr
Jan 21, 2020 at 6:52
• 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. Jan 21, 2020 at 7:04

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" *)