简体   繁体   中英

How do I convert float('nan') to binary in python?

The title says it all. I know pickle can do it, but I don't really want to open a file just to convert a single number. I want to take float('nan') and send it over the network in its binary format.

>>> import struct
>>> struct.pack('d', float('nan'))
b'\x00\x00\x00\x00\x00\x00\xf8\x7f'

if you want a double-precision (8-byte) binary representation in little-endian format. See https://docs.python.org/2/library/struct.html for all details of how to use big-endian and/or single-precision (4-byte) format instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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