How do I convert a Python float to a hexadecimal string in python 2.5? Nonworking solution attached

前端 未结 3 1231
太阳男子
太阳男子 2020-12-20 03:43

What I really need to do is to export a floating point number to C with no precision loss.

I did this in python:

import math
import struct
x = math.s         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-20 04:07

    If you're targeting a little-endian architecture,

    >>> s = struct.pack('>> ''.join('%.2x' % ord(c) for c in s)
    'cd3b7f669ea0f63f'
    

    if big-endian, use '>d' instead of . In either case, this gives you a hex string as you're asking for in the question title's, and of course C code can interpret it; I'm not sure what those two ints have to do with a "hex string".

提交回复
热议问题