简体   繁体   English

Delphi扩展到C#

[英]Delphi Extended to C#

how to convert a floating point 10 byte Hex string (Extended datatype in Delphi) to a C# datatype? 如何将浮点10字节十六进制字符串(Delphi中的扩展数据类型)转换为C#数据类型?

For example: 00 00 00 00 00 00 00 80 ff 3f is at Delphi 1 例如: 00 00 00 00 00 00 00 80 ff 3f在Delphi 1

Was involved in same issue, sharing my solution somebody can find useful: 参与了同样的问题,分享我的解决方案有人可以找到有用的:

        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);    

Code needs to be improved with NaN and Inf checks and probably "double" needs to be replaced by "decimal". 需要使用NaN和Inf检查来改进代码,并且可能需要将“double”替换为“decimal”。

Ok, here is my solution: 好的,这是我的解决方案:

Every string contains a factor byte at the second position. 每个字符串在第二个位置包含一个因子字节。 In my example the factor is ff . 在我的例子中,因子是ff

Now I have to convert the string via Floating-Point Conversion to decimal and multiply with the factor byte to get the result. 现在我必须通过浮点转换将字符串转换为十进制并与因子字节相乘以得到结果。

Example: 3f ff 80 00 00 (32bit) -> remove the factor byte (ff) -> 3f 80 00 00 -> convert to decimal -> result: 1 -> multiply with factor -> 1 * 1 -> result: 1 示例:3f ff 80 00 00(32位) - >删除因子字节(ff) - > 3f 80 00 00 - >转换为十进制 - >结果:1​​ - >乘以因子 - > 1 * 1 - >结果: 1

I hope this was helpfully 我希望这很有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM