作者eecheng87 (EEcheng)
看板C_and_CPP
標題[問題] 二進位轉float.double
時間Wed Nov 8 19:54:59 2017
如題,我想輸入010011......想得到output 為 浮點數
我的方法是 將0100....讀成字串(char陣列) 在讀陣列0,1..看看是0還是1
,在賦值給整數陣列,再拿這些整數陣列用IEEE的方法算回float,double.
這個方法感覺可行,想和各位大大請教是否有更smart的方法可以分享??
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.116.110.8
※ 文章網址: https://webptt.com/m.aspx?n=bbs/C_and_CPP/M.1510142102.A.CDA.html
1F:→ stupid0319: 用union呢, union{char a[4]; float b;}; 11/08 20:19
2F:推 TitanEric: float的話照IEEE 754就可以 但要注意32bit還是64bit 11/08 20:22
3F:→ TitanEric: 另外你上題不就問數字轉float 只是觀念翻轉而已 11/08 20:22
4F:→ eecheng87: 其實我遇到比較大的麻煩是要如何將讀到的0011..變成bit 11/08 21:04
5F:→ eecheng87: 應該說是比較好的方法轉成bit pattern 11/08 21:05
6F:→ Lipraxde: 就用你原本的方法阿 11/08 21:26
7F:推 remember: 先寫出來再改進XD 11/08 22:07
8F:→ remember: 不然你試試八個01構成的字串轉 char 當實驗啊 11/08 22:08
9F:推 remember: 剛查到有個東西叫std::bitset 11/08 22:17
10F:→ kingofsdtw: get as hex @@? 11/08 22:38
11F:推 tim00631: 這是資工大一程設的作業嗎哈哈哈我也有修 11/10 18:14
12F:推 tim00631: bitpattern轉float跟double可以使用標準庫裡的strtoul s 11/10 18:18
13F:→ tim00631: trtoull哦 11/10 18:18