dsuxcxqep31023992 2012-01-31 16:40
浏览 78
已采纳

如何将REAL48 float转换为double

I am connecting to a Pervasive SQL database which splits some data over two fields. DOUBLE fields are actually split into fieldName_1 and fieldName_2 where _1 is a 2 byte int and _2 is a 4 byte int.

I want to take these values and convert them using PHP into a usable value. I have some example code to do the conversion, but it is written in Delphi which I do not understand:

{ Reconstitutes a SmallInt and LongInt that form }
{ a Real into a double.                          }
Function EntConvertInts (Const Int2 : SmallInt;
                         Const Int4 : LongInt) : Double; StdCall;
Var
  TheRealArray : Array [1..6] Of Char;
  TheReal      : Real;
Begin
  Move (Int2, TheRealArray[1], 2);
  Move (Int4, TheRealArray[3], 4);
  Move (TheRealArray[1], TheReal, 6);

  Result := TheReal;
End;

Some data [fieldName_1,fieldName_2]

[132, 805306368] -> this should be 11

[132, 1073741824] -> this should be 12

I don't understand the logic enough to be able to port this into PHP. Any help would be most appreciated. Thanks

EDIT. This is the C code that they provided, showing sign/exponent:

double real_to_double (real r)
/* takes Pascal real, return C double */
{
    union doublearray da;
    unsigned x;

    x = r[0] & 0x00FF;  /* Real biased exponent in x */
    /* when exponent is 0, value is 0.0 */
    if (x == 0)
        da.d = 0.0;
    else {
        da.a[3] = ((x + 894) << 4) |  /* adjust exponent bias */
                  (r[2] & 0x8000) |  /* sign bit */
                  ((r[2] & 0x7800) >> 11);  /* begin significand */
        da.a[2] = (r[2] << 5) |  /* continue shifting significand */
                  (r[1] >> 11);
        da.a[1] = (r[1] << 5) |
                  (r[0] >> 11);
        da.a[0] = (r[0] & 0xFF00) << 5; /* mask real's exponent */
    }
    return da.d;
}
  • 写回答

5条回答 默认 最新

  • doufocheng6233 2012-02-03 21:44
    关注

    Adding this as another answer because I've finally figured this out. Here is PHP code which will convert the values. It has to be manually calculated because PHP does not know how to unpack a Real48 (non standard). Explanation in comments below.

    function BiIntToReal48($f1, $f2){
      $x = str_pad(decbin($f1), 16, "0", STR_PAD_LEFT);
      $y = str_pad(decbin($f2), 32, "0", STR_PAD_LEFT);
      //full Real48 binary string
      $real48 = $y . $x;
    
      //Real48 format is V = (-1)^s * 1.f * 2^(exp-129)
      // rightmost eight bits are the exponent  (bits 40-->47)
      // subtract 129 to get the final value
      $exp = (bindec(substr($real48, -8)) - 129);
    
      //Sign bit is leftmost bit (bit[0])
      $sign =$real48[0];
    
      //Now work through the significand - bits are fractional binary 
      //(1/2s place, 1/4s place, 1/8ths place, etc)
      // bits 1-->39 
      // significand is always 1.fffffffff... etc so start with 1.0
      $sgf = "1.0";
    
      for ($i = 1; $i <= 39; $i++){
          if ($real48[$i] == 1){
            $sgf = $sgf + pow(2,-$i); 
          }       
      } 
      //final calculation
      $final = pow(-1, $sign) * $sgf * pow(2,$exp);
      return($final);
    }
    $field_1 = 132;
    $field_2 = 805306368;      
    $ConvVal = BiIntToReal48($field_1, $field_2);
    // ^ gives $ConvVal = 11, qed
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作