Search code examples
javascriptasp-classicieee-754jscript

Reading a Windows 'binary' float into a ASP jscript variable


I need to read files produced by a legacy Windows app that stores real numbers (the 8-byte "double" type) in binary - i.e. as a packed array of 8 bytes. I can read the 8 byte group OK but how can I present it to my ASP JScript code such I can get the real number back again.

Or to put it another way:

Say a file was produced by a Windows (Delphi) program:

Assign (f, 'test.bin') ;
rewrite (f, 1) ;
r := 1234.56E78 ;
BlockWrite (f, r, SizeOf (Double)) ;
Close (f) ;

Inspection of the file will show it contains 8 bytes, being:

94 0E 4C CA C2 97 AD 53

which is the real number in IEEE format. Assuming I can read these 8 bytes back in ASP, is there a simple way of getting the real number back again?


Solution

  • Look at BitConverter.ToDouble().

    In an ASP.NET page, this could look like:

    <%@ Page Language="JScript"
        AutoEventWireup="true"
        CodeBehind="Default.aspx.cs"
        Inherits="WebApplication1._Default" %>
    <%
        var bytes: byte[] = [0x94, 0x0e, 0x4c, 0xca, 0xc2, 0x97, 0xad, 0x53];
        var d = BitConverter.ToDouble(bytes, 0);
        Response.Write(d);
    %>
    

    Which gets you the output:

    1.2345678E+95
    

    (You can check for correctness at http://babbage.cs.qc.edu/IEEE-754/)