Writing variables to a hex file in Python
Question: 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.


Answer: 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'
    

