1. 程式人生 > 其它 >IEEE754標準(Float和十六進位制轉換)

IEEE754標準(Float和十六進位制轉換)

原始資料
2080034220800343208003442080034520800046
2070034220200343218003442080134520810046 2060034220300343228003442080234520820046 2050034220400343238003442080334520830046 2040034220500343248003442080434520840046 2030034220600343258003442080534520850046
轉換後資料
32.8751220703125<-->131.50048828125<-->526.001953125    <-->2104.0078125<-->8224.03125
32.8594970703125<-->131.12548828125
<-->526.0020141601562<-->2360.0078125<-->8224.28125 32.8438720703125<-->131.18798828125<-->526.0020751953125<-->2616.0078125<-->8224.53125 32.8282470703125<-->131.25048828125<-->526.0021362304688<-->2872.0078125<-->8224.78125 32.8126220703125<-->131.31298828125
<-->526.002197265625 <-->3128.0078125<-->8225.03125 32.7969970703125<-->131.37548828125<-->526.0022583007812<-->3384.0078125<-->8225.28125
Python轉換程式碼
import re
import struct

def TextCov(file_path):
    temp_list = [] #建立一個空列表
    with open(file_path,"r",encoding="utf-8
") as f: #with語句有上下文管理的功能建立的控制代碼用完之後自己可以刪除關閉 line_Mum = f.readlines() #獲取檔案中的行數 for line in line_Mum: res = re.findall(r"\d{8}",line) #匹配對應行中任意8個字元 \d表示匹配任意數字0-9,長度為8 temp_list.append([struct.unpack('<f',bytes.fromhex(item))[0] for item in res]) #將十六進位制數轉換成float顯示格式(IEEE754標準) with open("./res.txt","w") as f: for item in temp_list: f.write(("<-->").join([str(item2)for item2 in item])) #join用來連結字串序列,連線符是<--> f.write("\n") if __name__ == "__main__": file_path = "./data.txt" TextCov(file_path)
有你相伴此生無憾!