drag n drop

調べたことを書きます

floatをバイト列で表示する

import struct

print '%08x' % struct.unpack('<L', struct.pack('>f', <数値>))[0]

 

例:

>>> print '%08x' % struct.unpack('<L',struct.pack('>f',1.0))[0]
0000803f

 

おまけ:

逆に、floatのバイト列の文字列から、10進数のfloatに戻す場合

print '%f' % struct.unpack('>f',struct.pack('<L',int(<文字列>,16)))[0]

 

参考:

7.3. struct — 文字列データをパックされたバイナリデータとして解釈する — Python 2.7ja1 documentation