栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python Datatype Conversions

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python Datatype Conversions

Bytes & Float Bytes to Float

​​​​​​python - Convert Bytes to Floating Point Numbers? - Stack Overflow

==> struct

Python struct pack, unpack - JournalDev

official doc: struct — Interpret bytes as packed binary data — Python 3.7.12 documentation

note:

e

(6)

float

2

(4)

4. For the 'f', 'd' and 'e' conversion codes, the packed representation uses the IEEE 754 binary32, binary64 or binary16 format (for 'f', 'd' or 'e' respectively), regardless of the floating-point format used by the platform.

6. The IEEE 754 binary16 “half precision” type was introduced in the 2008 revision of the IEEE 754 standard. It has a sign bit, a 5-bit exponent and 11-bit precision (with 10 bits explicitly stored), and can represent numbers between approximately                         ​​​​​​​        ​​​​​​​6.1e-05 and 6.5e+04 at full precision. This type is not widely supported by C compilers: on a typical machine, an unsigned short can be used for storage, but not for math operations. See the Wikipedia page on the half-precision floating-point format for more information.

Float & Hex (String) Float to Hex String

python - Convert numpy array of float32 data type to hex format - Stack Overflow

Hex String to Float

fromhex — Python Reference (The Right Way) 0.1 documentation

"Fake" Hex (Bytes) to Float

a hex string in the example below is really just a string of bytes in hexadecimal representation

python - Convert hex to float - Stack Overflow

==> fromhex() is a class specific method ==> the best answer in the example use bytes.fromhex() to get a packed buffer, while float.fromhex() requires a entirely different style of hex string. 

Float (or Any) to "Fake" Hex

suggestion: use numpy.ndarray.tobytes() ==> give python bytes and can convert to bytes in hex form by:

        result = "0x"
		for byte in list(datum.tobytes())[::-1]:
			result += hex(byte)[2:].zfill(2) #byte converts to 2 hex. digits
		return result
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/664549.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号