Python Datatype Conversions

本文详细介绍了Python中数值类型之间的转换方法,包括Int转Bytes使用to_bytes(),Bytes转Float利用struct模块,以及Float转换为十六进制字符串。还讨论了不同转换过程中的参数和注意事项,如字节顺序和浮点数的二进制表示。此外,还提供了将Bytes转换为Float的技巧以及如何将Float转换为“伪”十六进制字符串的步骤。

Bytes & Int

Int to Bytes

1. to_bytes()

from: How to Convert Int to Bytes in Python? - GeeksforGeeks

Syntax: int.to_bytes(length, byteorder)

Arguments : 

length – desired length of the array in bytes .

byteorder – order of the array to carry out conversion of an int to bytes. byteorder can have values as either “little” where most significant bit is stored at the end and least at the beginning, or big, where MSB is stored at start and LSB at the end. 

Exceptions : 

OverflowError is returned in case the integer value length is not large enough to be accommodated in the length of the array. 

byteorder = 'big' or 'little'

the default python byte printing order is ascending, ==> so use 'big' for a "normal" writing order view. 

for numpy, see: numpy.dtype.byteorder — NumPy v1.21 Manual

2. int to str, then str.encode()

or use bytes(), see: Python bytes()

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.asarray(datum) to convert a single python base type input to a numpy type;

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值