Re: Converting decimal to/from IEEE hex format
- To: mathgroup at smc.vnet.net
- Subject: [mg71239] Re: [mg71206] Converting decimal to/from IEEE hex format
- From: Carl Woll <carlw at wolfram.com>
- Date: Sat, 11 Nov 2006 03:38:58 -0500 (EST)
- References: <200611101138.GAA13743@smc.vnet.net>
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] ] 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
- Follow-Ups:
- Re: Re: Converting decimal to/from IEEE hex format
- From: Carl Woll <carlw@wolfram.com>
- Re: Re: Converting decimal to/from IEEE hex format
- References:
- Converting decimal to/from IEEE hex format
- From: "Robert" <renenkel@hotmail.com>
- Converting decimal to/from IEEE hex format