Re: Re: Converting decimal to/from IEEE hex format
- To: mathgroup at smc.vnet.net
- Subject: [mg71261] Re: [mg71239] Re: [mg71206] Converting decimal to/from IEEE hex format
- From: Carl Woll <carlw at wolfram.com>
- Date: Sun, 12 Nov 2006 06:48:00 -0500 (EST)
- References: <200611101138.GAA13743@smc.vnet.net> <200611110838.DAA27553@smc.vnet.net>
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
>
>
- References:
- Converting decimal to/from IEEE hex format
- From: "Robert" <renenkel@hotmail.com>
- Re: Converting decimal to/from IEEE hex format
- From: Carl Woll <carlw@wolfram.com>
- Converting decimal to/from IEEE hex format