python不用struct 十六进制转换float
2021/8/2 11:06:04
本文主要是介绍python不用struct 十六进制转换float,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
自用记录
def hexstr2ieee745(hexStr): ret = str(); for x in range(0,len(hexStr),2): a = hexStr[x:x+2]; intItem = int(a,16); binnaryStr = bin(intItem)[2:len(bin(intItem))]; binnaryStr = "%(binnary)08d"%{'binnary':int(binnaryStr)} ret = ret+binnaryStr; s= int(ret[0]); n = int(ret[1:9],2); mStr = ret[9:len(ret)-1]; m = float(); print(mStr); for x in range(1,len(mStr)-1,1): if mStr[x-1] == "1": print(x); m=m+math.pow(0.5,x); val = math.pow(-1,s)*(math.pow(2,n-127))*(1+m); print(val) return ret;
public static double hexstr2ieee745(String hexStr){ StringBuffer binaryStr = new StringBuffer(); for(int i=0;i< hexStr.length();i+=2){ String a = hexStr.substring(i,i+2); int c = Integer.parseInt(a,16); String item = String.format("%08d",Integer.parseInt(Integer.toBinaryString(c))); binaryStr.append(item); } int n = Integer.parseInt(binaryStr.substring(1,9),2); String mStr = binaryStr.substring(9,binaryStr.length()-1); double sum = 0; for(int i =1;i<=mStr.length();i++){ if(mStr.charAt(i-1)=='1'){ sum = sum+Math.pow(0.5,i); } } double a =( Math.pow(2,n-127))*(1+sum); return a; }
转自:https://www.cnblogs.com/bingoj/p/11148305.html
这篇关于python不用struct 十六进制转换float的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门
- 2024-11-14Python编程入门指南
- 2024-11-13Python基础教程
- 2024-11-12Python编程基础指南
- 2024-11-12Python基础编程教程
- 2024-11-08Python编程基础与实践示例
- 2024-11-07Python编程基础指南
- 2024-11-06Python编程基础入门指南
- 2024-11-06怎么使用python 计算两个GPS的距离功能-icode9专业技术文章分享