MathGroup Archive 2006

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

Search the Archive

Re: Re: Converting decimal to/from IEEE hex format


Carl Woll wrote:

>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
>> 
>>
>>    
>>
>One idea is to use BinaryWrite and BinaryRead, although that requires 
>the use of external files. Here is one implementation:
>
>RealToHex32[x_Real?MachineNumberQ]:=Module[{tmp},
>   tmp=OpenTemporary[BinaryFormat->True];
>   BinaryWrite[tmp,x,"Real32"];
>   tmp=Close[tmp];
>   BaseForm[BinaryRead[tmp,"Integer32"],16]
>]
>  
>
Oops, that should have been "UnsignedInteger32" not "Integer32".

Carl Woll
Wolfram Research

>In[31]:=
>RealToHex32[1.0] // OutputForm
>Out[31]//OutputForm=
>3f800000
>        16
>
>The conversion backwards needs a design decision on what format you want 
>the hex representation to be in.
>
>Carl Woll
>Wolfram Research
>  
>


  • Prev by Date: Re: finite group theory w/mathematica
  • Next by Date: Re: change of variables
  • Previous by thread: Re: Converting decimal to/from IEEE hex format
  • Next by thread: Re: Converting decimal to/from IEEE hex format