How to convert a float into hex

后端 未结 4 1488
情话喂你
情话喂你 2020-12-02 21:02

In Python I need to convert a bunch of floats into hexadecimal. It needs to be zero padded (for instance, 0x00000010 instead of 0x10). Just like http://gregstoll.dyndns.org/

4条回答
  •  無奈伤痛
    2020-12-02 21:55

    Further to Jonathon Reinhart's very helpful answer. I needed this to send a floating point number as bytes over UDP

    import struct
    
    # define double_to_hex (or float_to_hex)
    def double_to_hex(f):
        return hex(struct.unpack('d', doubleAsBytes)[0] # or '>f' for float_to_hex
    

提交回复
热议问题