简体   繁体   中英

Writing variables to a hex file in Python

So, I have got a float value: -1.0f, or something. And how could I write it into a file in hexadecimal format in Python? I mean that we open the file in notepad, we won't see the hexadecimal values, just the ASCII code.

In Python 3:

>>> import struct
>>> "".join("{0:02X}".format(b) for b in struct.pack(">f", -1.0))
'BF800000'

In Python 2:

>>> import struct
>>> "".join("{0:02X}".format(ord(b)) for b in struct.pack(">f", -1.0))
'BF800000'

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