Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Get a string representing the hexadecimal form of a real number
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. |
Find the hexadecimal string for the decimal 1.1:
| In[1]:= |
| Out[1]= |
RealToHexString will handle arbitrary-precision reals:
| In[2]:= |
| Out[2]= |
RealToHexString can return a string with length exceeding the capacity of a machine double-precision value:
| In[3]:= |
| Out[3]= |
RealToHexString returns a string of all zeros for inputs that are numeric zeros:
| In[4]:= |
| Out[4]= |
| In[5]:= |
| Out[5]= |
A small number:
| In[6]:= |
| Out[6]= |
Convert it to a hex string with 20 hexadecimal digits and a mantissa of 16 bits:
| In[7]:= |
| Out[7]= |
Convert its reciprocal:
| In[8]:= |
| Out[8]= |
Check that they convert back and give the expected product:
| In[9]:= |
| Out[9]= |
Start with a number:
| In[10]:= |
| Out[10]= |
Use the resource function "HexStringToReal" to reverse the process, effectively round-tripping from RealToHexString:
| In[11]:= |
| Out[12]= |
RealToHexString will pad with zeros if the number of hex digits to return makes it exceed the internal precision of the input:
| In[13]:= |
| Out[13]= |
Wolfram Language 11.3 (March 2018) or above
This work is licensed under a Creative Commons Attribution 4.0 International License