将C#double转换为Delphi Real48

将C#double转换为Delphi Real48,c#,delphi,double,C#,Delphi,Double,我发现了以下问题,但我想换一种方式,从C#到Delphi 有人知道如何做到这一点吗?我曾尝试过对代码进行反向工程,但运气不太好 更新: 我在寻找C#代码,它将接受一个double并将其转换为Real48(字节[]大小为6) 谢谢如果您熟悉C(当您使用C#编写时,您应该会很好),请查看此函数。把它移到C#应该不会太难 这很难看,但我认为是必要的 参考: enum prconverr double_to_real(双d,real*r) /*将C double转换为Pascal real,返回错误代码

我发现了以下问题,但我想换一种方式,从C#到Delphi

有人知道如何做到这一点吗?我曾尝试过对代码进行反向工程,但运气不太好

更新:

我在寻找C#代码,它将接受一个double并将其转换为Real48(字节[]大小为6)

谢谢

如果您熟悉C(当您使用C#编写时,您应该会很好),请查看此函数。把它移到C#应该不会太难

这很难看,但我认为是必要的

参考:

enum prconverr double_to_real(双d,real*r)
/*将C double转换为Pascal real,返回错误代码*/
{
联合双阵列da;
无符号x;
da.d=d;
/*检查0.0*/
如果((da.a[0]==0x0000)&&
(da.a[1]==0x0000)&&
(da.a[2]==0x0000)&&
/*忽略符号位*/
((da.a[3]&0x7FFF)==0x0000)){
/*指数和有效位均为0,因此值为0.0*/
(*r)[2]=(*r)[1]=(*r)[0]=0x0000;
/*符号位被忽略(-0.0->0.0)*/
返回项目;
}
/*最大指数值检验*/
如果((da.a[3]&0x7FF0)==0x7FF0){
/*值为Inf或NaN*/
如果((da.a[0]==0x0000)&&
(da.a[1]==0x0000)&&
(da.a[2]==0x0000)&&
((da.a[3]&0x000F)==0x0000)){
/*有效位为0,因此值为Inf*/
/*值变为有符号最大实数*/
/*然后返回错误代码prInf*/
(*r)[1]=(*r)[0]=0xFFFF;
(*r)[2]=0x7FFF|
(da.a[3]&0x8000);/*保留符号位*/
返回prInf;
}否则{
/*有效位不是0,所以值是NaN*/
/*值变为0.0,并返回prNaN代码*/
/*忽略符号位(无负NaN)*/
(*r)[2]=(*r)[1]=(*r)[0]=0x0000;
/*符号位被忽略(-NaN->+NaN)*/
返回prNaN;
}
}
/*如有必要,将有效位四舍五入*/
如果((da.a[0]&0x1000)==0x1000){
/*有效位的第40位设置,因此将有效位向上舍入*/
如果((da.a[0]&0xE000)!=0xE000)
/*增加3个最高有效位的空间*/
da.a[0]+=0x2000;
否则{
/*进位到下一个元素*/
da.a[0]=0x0000;
/*从第0个元素到第1个元素的进位*/
如果(da.a[1]!=0xFFFF)
da.a[1]++;
否则{
da.a[1]=0x0000;
/*从第一个元素到第二个元素的进位*/
如果(da.a[2]!=0xFFFF)
da.a[2]++;
否则{
da.a[2]=0x0000;
/*从第二个元素到第三个元素的进位*/
/*有效位可能溢出到指数中*/
/*指数未满,因此不会溢出*/
da.a[3]++;
}
}
}
}
/*获取下溢/溢流测试的指数*/
x=(da.a[3]&0x7FF0)>>4;
/*底流试验*/
if(x<895){
/*该值低于实际范围*/
(*r)[2]=(*r)[1]=(*r)[0]=0x0000;
如果((da.a[3]&0x8000)==0x8000)
/*设置了符号位,因此值为负数*/
回流;
其他的
/*未设置符号位*/
回流;
}
/*溢流测试*/
如果(x>1149){
/*值高于实际范围*/
(*r)[1]=(*r)[0]=0xFFFF;
(*r)[2]=0x7FFF |(da.a[3]&0x8000);/*保留符号位*/
回流阀;
}
/*值在实际范围内*/
(*r)[0]=(x-894)|/*re偏差指数*/
((da.a[0]&0xE000)>>5)|/*开始有效位*/
(da.a[1]>5)|
(da.a[2]>5)|

((da.a[3]&0x000F)如果可能的话,最简单的方法是将其转换为字符串,传递,然后将其转换回Real48

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

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;

}
公共静态字节[]Double2Real48(双d)
{
字节[]r48=新字节[6];
字节[]da=位转换器.GetBytes(d);
for(int i=0;i=4;
n |=b2;
如果(n==0)
返回r48;
字节ex=(字节)(n-1023);
r48[0]=(字节)(ex+129);
//复制尾数
r48[5]|=(字节)((da[6]&0x0f)>5);//获取前三位
r48[4]=(字节)((da[5]&0x1f)>5);//获取前三位
r48[3]=(字节)((da[4]&0x1f)>5);//获取前三位
r48[2]=(字节)((da[3]&0x1f)>5);//获取前三位
r48[1]=(字节)((da[2]&0x1f)>5);//获取前三位
返回r48;
}
Real48与ieee754相似,尾数相同。为了使尾数位于正确的位置,必须进行位移位

Real48指数的偏差为129,双精度指数的偏差为1023

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

注意事项: 我认为这段代码在big-endian机器上不起作用。它不检查NAN或INF

下面是将real48转换为double的代码。它是从免费的Pascal编译器移植的:

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);  
}
static double real2double(字节[]r)
{
字节[]res=新字节[8];
整数指数;
//如果指数为零,则返回零
if(r[0]==0)
返回(双)0;
//复制尾数
res[0]=0;
res[1]=(字节)(r[1]>3)|(r[2]>3)|(r[3]>3)|(r[4]>3)|((r[5]&0x7f)>3);
//复制指数
//正确指数
指数=(r[0]+(1023-129));
res[6]=(字节)(res[6]|((指数&0xf)>
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);  
}
double Double_Real48(double d)
{
  unsigned long long r48 = 0, tmp;

  tmp = *(long long *)&d;//m
  tmp/=0x20;
  tmp&=0x7FFFFFFFFF00;
  r48+=tmp;

  tmp = *(long long *)&d;//e
  tmp/=0x10000000000000;
  tmp-=894;
  tmp&=0xFF;
  if (tmp == 0) return 0.0;
  r48+=tmp;

  tmp = *(long long *)&d;//s
  tmp/=0x10000;
  tmp&=0x800000000000;
  r48+=tmp;

  return *(double *)&r48;
}

double Real48_Double(double r48)
{
  unsigned long long d = 0, tmp;

  tmp= *(long long *)&r48;//m
  tmp&=0x7FFFFFFFFF00;
  tmp*=0x20;
  d+=tmp;

  tmp= *(long long *)&r48;//e
  tmp&=0xFF;
  if (tmp == 0) return 0.0;
  tmp+=894;
  tmp*=0x10000000000000;
  d+=tmp;

  tmp= *(long long *)&r48;//s
  tmp&=0x800000000000;
  tmp*=0x10000;
  d+=tmp;

  return *(double *)&d;
}
typedef struct {
   unsigned char exponent;  // 8 bites;
   unsigned long mantisaLo; // 32 of 39 bites
   unsigned char mantisaHi : 7, sign : 1;  // 7 of 39 bites
} T_Real48;

typedef struct {
   unsigned long mantisaLo; // 32 of 52 bites
   unsigned long mantisaHi:20, exponent: 11, sign : 1; // 20 of 52 bites
} T_Double64;

double doubleToReal48(double val)
{
  T_Real48 real48;
  T_Double64 *double64 = (T_Double64*) &val;

  real48.mantisaHi = double64->mantisaHi >> 13;
  real48.mantisaLo =(double64->mantisaLo >> 13) + ((double64->mantisaHi & 0x1FFF) << 19);
  real48.exponent  = double64->exponent - 894;
  real48.sign      = double64->sign;

  if (real48.exponent == 0) {
      real48.mantisaHi = 0;
      real48.mantisaLo = 0;
  }

  return *(double *)&real48;
}

double real48ToDouble(double val)
{
  T_Real48 *real48 = (T_Real48*) &val;
  T_Double64 double64;

  double64.mantisaHi = (real48->mantisaHi << 13) + (real48->mantisaLo >> 19);
  double64.mantisaLo = real48->mantisaLo << 13;
  double64.exponent  = real48->exponent + 894;
  double64.sign      = real48->sign;

  return *(double *)&double64;
}