简体   繁体   English

如何在 Python 中将十六进制数转换为浮点数

[英]how to convert a hex number to float in Python

how to convert a hex number to float i use this in C如何将十六进制数转换为浮点数我在 C 中使用它

HexTofloat float (uint32_t x){
   return (* (float *) & x);
}
i = HexTofloat (1130839281);
print (i); //result 231.24

How does it work?它是如何工作的? i want to make it in python我想在 python

You must use float.fromhex and float.hex您必须使用float.fromhexfloat.hex

float.fromhex('0x1.5c28f5c28f5c3p-1')
Output: 0.68

you can use in-built function of python like this: float.hex(num)您可以像这样使用 python 的内置 function : float.hex(num)

You can use a library which is known as ast您可以使用称为ast的库
To install this library you need to type:- pip install AST要安装此库,您需要键入:- pip install AST
eg:-例如:-

from ast import literal_eval 
data = "0xAB15"
out = literal_eval(data) 
print(out)

you will get the output:- 43797你会得到 output:- 43797

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM