Python和C两种方式实现---IEEE754数据十六进值数与浮点数转换方式

工作中经常需要针对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 }

注:这里附加一个网页版在线工具

 

https://www.h-schmidt.net/FloatConverter/IEEE754.html

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只特立独行的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值