工作中经常需要针对IEEE754数据进行处理:主要是十六进制数据转换为对应的浮点数,虽然有网页版或免安装版的工具可以直接实现其需求,但大都只能实现单个数据的转换,对于批量处理数据来讲不易用工具实现。
1、本文借助python中的struct内嵌库实现上述功能,详细代码如下:
# -*- coding: cp936 -*-
import struct #16进制转换浮点型
import os
import sys
import xlwt
import xlrd
#from xlrd import *
def learn_to_pack_func():
''
while(1):
op_id = int(raw_input('please select float to hex(0) or hex to float(1),other value to quit:'))
if op_id == 0:
num = float(raw_input('please input the float number:'))
result = struct.pack('>f',num).encode('hex')
print result
elif op_id== 1:
num =str(raw_input('please input the hex number:'))
str1= num[2:]
str2 =struct.unpack('>f',(str1.decode('hex')))
print str2
else:
break
return 0
def read_data_from_excel(filePath,readData):
''
#read data from excel
bk = xlrd.open_workbook(filePath)
shxrange = range(bk.nsheets)
try:
sh = bk.sheet_by_name('Sheet1')
except:
print "no sheet in %s named Sheet1" % fname
#获取行数
nrows = sh.nrows
#获取列数
ncols = sh.ncols
#获取数据
for i in range(0,nrows):
for j in range(0,ncols):
readData.append(sh.cell(i,j).value)
return 0
def save_data_to_excel(filePath,writeData):
''
bk = xlwt.Workbook(encoding='utf-8')
ws = bk.add_sheet('Sheet1',cell_overwrite_ok=False)
for i,row in enumerate(writeData):
for j,clo in enumerate(row):
ws.write(i,j,clo)
bk.save(filePath)
def LEEE754Data_Hex_To_Float_Translate():
''
cwd = os.getcwd()
path = cwd +'\data.xls'
path_1 = cwd+'\data_1.xls'
Data_Source = []
Data_Result = []
#获取数据
read_data_from_excel(path,Data_Source)
#数据转换
for i in range(len(Data_Source)):
num =Data_Source[i]
str1= num[2:]
str2 =struct.unpack('>f',(str1.decode('hex')))
Data_Result.append(str2)
#保存数据
save_data_to_excel(path_1,Data_Result)
return 0
def main():
''
op_mode =int(raw_input('please select opreation mode(0:auto,1:manual):'))
if 0 == op_mode:
LEEE754Data_Hex_To_Float_Translate()
elif 1 == op_mode:
learn_to_pack_func()
else:
print 'error'
if __name__=='__main__':
main()
2、C方式实现
#include<stdio.h>
#include<assert.h>
30 void float_to_ieee_int(float* pfnum,int* pinum)
31 {
32 assert(NULL!=pfnum);
33 assert(NULL!=pinum);
34
35 float ftemp=0;
36 ftemp=*pfnum;
37 int* iptemp=(int*)&ftemp;
38 *pinum=*iptemp;
39 }
40
41 void ieee_int_to_float(float* pfnum,int* pinum)
42 {
43 assert(NULL!=pfnum);
44 assert(NULL!=pinum);
45
46 int temp=0;
47 temp=*pinum;
48 float* fptemp=(float*)&temp;
49 *pfnum =*fptemp;
50 }
注:这里附加一个网页版在线工具