Re: [問題] hex to float疑問

看板C_and_CPP作者 (批踢踢世界)時間9年前 (2016/10/15 17:55), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/4 (看更多)
※ 引述《Hao0228 (Hao0228)》之銘言: : 各位版大好...想請教一個問題 : 假設我想指定一個hex給一個float變數 : 比如說...0xBF9DF3B6 在float內是 -1.234 : 寫了一段測試的程式 : uint32_t i = 0xBF9DF3B6; : float f = *(float*)&i; : cout << f << endl; : float f2 = 0xBF9DF3B6; : cout << f2 << endl; : 執行結果 : f : -1.234 : f2 : 3.2148e+09 : 一直搞不太懂為什麼非得用float *轉型呢? : 還請各位版大能為小弟解惑...謝謝!! 建議原po清楚以下程式的觀念。 #include <iostream> #include <cstdint> int main() { short j = (short)0xF3B6; std::cout << (int)j << std::endl; std::cout << (uint32_t)j << std::endl; float f = *(float*)&j; std::cout << f << std::endl; double f2 = j; std::cout << f2 << std::endl; return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 115.43.36.13 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1476525300.A.968.html
文章代碼(AID): #1O0Vpqbe (C_and_CPP)
文章代碼(AID): #1O0Vpqbe (C_and_CPP)