Function Repository Resource:

RealToHexString

Source Notebook

Give a string representing the hexadecimal form of a real number

Contributed by: Daniel Lichtblau

ResourceFunction["RealToHexString"][n,{e,h}]

gives the hexadecimal string corresponding to n, with an exponent field of e bits and a total length of h hex digits.

ResourceFunction["RealToHexString"][n]

gives the hexadecimal string for n, using the common IEEE double-precision values of 11 exponent bits and 64 bits total.

Details and Options

ResourceFunction["RealToHexString"] uses rounding on arbitrary-precision inputs in order to get the final digit correct.

Examples

Basic Examples (1) 

Find the hexadecimal string for the decimal 1.1:

In[1]:=
ResourceFunction["RealToHexString"][1.1]
Out[1]=

Scope (2) 

RealToHexString will handle arbitrary-precision reals:

In[2]:=
ResourceFunction["RealToHexString"][N[11/10, 20]]
Out[2]=

RealToHexString can return a string with length exceeding the capacity of a machine double-precision value:

In[3]:=
ResourceFunction["RealToHexString"][N[11/10, 20], {11, 20}]
Out[3]=

Possible Issues (1) 

RealToHexString will pad with zeros if the number of hex digits to return makes it exceed the internal precision of the input:

In[4]:=
ResourceFunction["RealToHexString"][N[11/10, 20], {11, 54}]
Out[4]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 28 February 2019

License Information