如何將 10 字節 Long 類型的值的 Double 轉換爲 8 字節 Double

與在 16 位 Microsoft C/C++ 編譯器 long double 存儲爲 80 位 (10 個字節) 的數據類型。 在 Windows NT 下爲了與其他非 Intel 的浮點實現,兼容 80 位長雙格式是別名爲 64 位 (8 字節) 雙格式。

這意味着 32 位程序可能不是可以讀取的後數據文件寫入由 16 位程序,因爲長雙格式不兼容。

在 Intel 平臺上唯一的解決方法是讓浮點處理器句柄從 80 位到 64 位精度。 以後,數據到在 Win 32 下使用 64 位 Double 類型存儲。

下面的代碼示例說明了您可以使用浮點指令在內聯程序集中將從 10 字節 Double 類型的數據文件轉換爲一個 8 字節雙。

示例代碼

<script type="text/javascript"></script>

/* Compile options needed: none
*/ 

#include <stdio.h>

void main(void)
{
   FILE *inFile;
   char buffer[10];
   long double Newdbl;

   inFile = fopen("data","rb");
   fread(buffer, 10, 1, inFile);      // reads in 10-byte long double
   fclose(inFile);

   // This moves the contents of the buffer into the floating point
   // register, which then then takes care of the automatic convertion
   // back to a 8-byte long double

   _asm {
      fld TBYTE PTR buffer;
      fstp Newdbl;
   }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章