2013-09-21 121 views
0

在我的項目我有十六進制值(大端)將十六進制轉換爲二進制到十六進制?

QString hex_in("413DF3EBA463B0"); 

我怎麼能轉換hex_in爲圓角雙爲QString? IEEE 754(https://en.wikipedia.org/wiki/Double_precision_floating-point_format

34.5 

用戶將編輯的雙,然後我的程序需要將其轉換回爲十六進制。

感謝您的時間:)

+2

如何是十六進制字符串和雙相關?字符串是double的二進制內存佈局的表示嗎?什麼編碼? IEEE? – IInspectable

+0

對不起,IEEE 754.這個字符串是double的十六進制表示。 – mrg95

+0

Big Endian或Little Endian? – IInspectable

回答

5

實在是隻有一個辦法做到這一點,那就是將字符串轉換爲整數,把它放在你設置一個整數構件union和讀出double的成員。

對於字符串轉換,您可以使用例如one of these functions


示例代碼:

double hexstr2double(const std::string& hexstr) 
{ 
    union 
    { 
     long long i; 
     double d; 
    } value; 

    value.i = std::stoll(hexstr, nullptr, 16); 

    return value.d; 
} 

// ... 

std::cout << "413DF3EBA463B0 = " << hexstr2double("413DF3EBA463B0") << '\n'; 

代碼的輸出將是

 
413DF3EBA463B0 = 1.91824e-307 
+0

我不是將字符串轉換爲數字,我想要字符串表示的十六進制數字轉換爲數字。除非我誤解了你的答案? – mrg95

+0

@ mc360pro但是沒有辦法知道一個字符串可能代表什麼數字,而沒有將字符串*轉換爲*該數字。 –

+0

這就是我想要做的。將此十六進制值轉換爲數字。不會轉換隻是字符串轉換爲雙重轉換ASCII字符或東西,而不是實際的十六進制值是什麼意思? – mrg95

0
double HexToDouble(AnsiString str) 
{ 
    double hx ; 
    int nn,r; 
    char * ch = str.c_str(); 
    char * p,pp; 
    for (int i = 1; i <= str.Length(); i++) 
    { 
    r = str.Length() - i; 
    pp = ch[r]; 
    nn = strtoul(&pp, &p, 16); 
    hx = hx + nn * pow(16 , i-1); 
    } 
    return hx; 
} 

我爲大十六進制位功能

結果

72850ccbb88c6226afed9d8d971c8938  -->  1.5222282653101E+38  
000015d85a903c72b6bebdd18fb26811  -->  4.4307191280143E+32 
相關問題