MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Converting decimal to/from IEEE hex format


Robert wrote:
> Can anyone tell me how to convert a number to/from the corresponding
> IEEE hex floating-point representation in either 32 or 64 bits in
> Mathematica?  For
> example, I would want to convert 1.0 into 3F800000 (32-bit) or
> 3FF0000000000000 (64-bit), and vice-versa.  (When converting from
> decimal to hex, if the number is not exactly representable, I would
> want the nearest floating-point number.)  Thanks, Robert

In[1]:=
ieee64[x_] := StringJoin[ StringTake["0123456789ABCDEF",{#+1}]& /@
Join[IntegerDigits[2048*Boole@Negative@x+1022+#[[2]],16,3],
FromDigits[#,2]&/@Partition[Rest@#[[1]],4]]& @ RealDigits[N@x,2,53] ]

In[2]:= ieee64[-1.75]

Out[2]= BFFC000000000000


  • Prev by Date: change of variables
  • Next by Date: Too late to upgrade from 4.2 to 5.2....
  • Previous by thread: Re: Re: Converting decimal to/from IEEE hex format
  • Next by thread: Question Solve