Delphi Extended to C #

how to convert string with Hex floating point byte (Extended datatype in Delphi) to C # data type?

For example: 00 00 00 00 00 00 00 80 80 ff 3f is in Delphi 1

+3
source share
2 answers

Ok, here is my solution:

Each line contains a factor byte in the second position. In my example, the coefficient is ff .

Now I need to convert the string through Conversion from floating point to decimal number and multiply by byte coefficient to get the result.

: 3f ff 80 00 00 (32 ) → (ff) → 3f 80 00 00 → → : 1 → → 1 * 1 → : 1

,

+1

, , - :

        var extendedSize = 10;
        var buf = new byte[extendedSize];

        // Populate buffer with something like: { 0x00, 0x68, 0x66, 0x66, 0x66, 0x66, 0x66, 0xA2, 0x02, 0x40 } = 10.15
        // Read(buf, extendedSize);

        var sign = (buf[extendedSize - 1] & 0x80) == 0x80 ? -1 : 1;
        buf[extendedSize - 1] = (byte)(buf[extendedSize - 1] & 0x7F);
        var exp = BitConverter.ToUInt16(buf, extendedSize - 2);
        var integral = (buf[extendedSize - 3] & 0x80) == 0x80 ? 1 : 0;           

        // Calculate mantissa
        var mantissa = 0.0;
        var value = 1.0;
        var fractal = BitConverter.ToUInt64(buf, 0);

        while (fractal != 0)
        {
            value = value / 2;
            if ((fractal & 0x4000000000000000) == 0x4000000000000000) // Latest bit is sign, just skip it
            {
                mantissa += value;
            }
            fractal <<= 1;
        }

        return sign * (Math.Pow(2, exp - 16383)) * (integral + mantissa);    

NaN Inf, , , "double" "decimal".

+2

Source: https://habr.com/ru/post/1747845/


All Articles