转换C#双德尔福Real48 [英] Convert C# double to Delphi Real48

查看:140
本文介绍了转换C#双德尔福Real48的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现以下问题转换德尔福Real48到C#双但我想要去的其他方式,C#德尔福。

I've found the following question Convert Delphi Real48 to C# double but I want to go the other way, C# to Delphi.

有谁知道如何可以做到这一点?我已经试过逆向工程的代码,但没有多少运气

Does anyone know how this can be done? I've tried reverse engineering the code but without much luck.

更​​新:

在C#我代码,将采取双重并将其转换成一个Real48(字节[大小6])。

I'm after C# code that will take a double and convert it into a Real48 (byte[] of size 6).

感谢

推荐答案

我碰到这个线程寻找相同的代码。以下是我最后写:

I came across this thread looking for the same code. Here is what I ended up writing:

public static byte [] Double2Real48(double d)
{
    byte [] r48 = new byte[6];
    byte [] da = BitConverter.GetBytes(d);

    for (int i = 0; i < r48.Length; i++)
        r48[i] = 0;

    //Copy the negative flag
    r48[5] |= (byte)(da[7] & 0x80);

    //Get the expoent
    byte b1 = (byte)(da[7] & 0x7f);
    ushort n = (ushort)(b1 << 4);
    byte b2 = (byte)(da[6] & 0xf0);
    b2 >>= 4;
    n |= b2;

    if (n == 0)
        return r48;

    byte ex = (byte)(n - 1023);
    r48[0] = (byte)(ex + 129);

    //Copy the Mantissa
    r48[5] |= (byte)((da[6] & 0x0f) << 3);//Get the last four bits
    r48[5] |= (byte)((da[5] & 0xe0) >> 5);//Get the first three bits

    r48[4]  = (byte)((da[5] & 0x1f) << 3);//Get the last 5 bits
    r48[4] |= (byte)((da[4] & 0xe0) >> 5);//Get the first three bits

    r48[3]  = (byte)((da[4] & 0x1f) << 3);//Get the last 5 bits
    r48[3] |= (byte)((da[3] & 0xe0) >> 5);//Get the first three bits

    r48[2]  = (byte)((da[3] & 0x1f) << 3);//Get the last 5 bits
    r48[2] |= (byte)((da[2] & 0xe0) >> 5);//Get the first three bits

    r48[1]  = (byte)((da[2] & 0x1f) << 3);//Get the last 5 bits
    r48[1] |= (byte)((da[1] & 0xe0) >> 5);//Get the first three bits

    return r48;

}



Real48是在类似于IEEE 754尾数会一样。位移位是必要的,以获得尾数在正确的位置。

Real48 is similar to IEEE 754 in that the Mantissa will be the same. The bit shift are necessary to get the Mantissa in the right location.

Real48指数具有129的偏压和双具有1023的偏置

Real48 exponent has a bias of 129 and the double has a bias of 1023.

负标志被存储在最后一个字节的第一位。

The negative flag is stored in the first bit of the last byte.

备注:
我不认为这个代码将大端机器上工作。它不检查NAN或INF。

Notes: I don't think this code will work on a big endian machine. It does not check for NAN or INF.

下面是一个转换到real48双代码。它是从Free Pascal编译器移植:

Here is the code that converts a real48 to a double. It was ported from the Free Pascal compiler:

static double real2double(byte [] r)
{
    byte [] res = new byte[8];
    int exponent;

    //Return zero if the exponent is zero        
    if (r[0] == 0)
        return (double)0;

    //Copy Mantissa
    res[0] = 0;
    res[1] = (byte)(r[1] << 5);
    res[2] = (byte)((r[1] >> 3) | (r[2] << 5));
    res[3] = (byte)((r[2] >> 3) | (r[3] << 5));
    res[4] = (byte)((r[3] >> 3) | (r[4] << 5));
    res[5] = (byte)((r[4] >> 3) | ((r[5] & 0x7f) << 5));
    res[6] = (byte)((r[5] & 0x7f) >> 3);

    //Copy exponent
    //correct exponent
    exponent = (r[0] + (1023-129));
    res[6] = (byte)(res[6] | ((exponent & 0xf) << 4));
    res[7] = (byte)(exponent >> 4);

    //Set Sign
    res[7] = (byte)(res[7] | (r[5] & 0x80));
    return BitConverter.ToDouble(res, 0);  
}

这篇关于转换C#双德尔福Real48的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆