fork download
  1. import struct
  2.  
  3. def float_to_bin(value):
  4. # IEEE 754準拠の倍精度浮動小数点数をバイナリ形式に変換する
  5. packed = struct.pack("!d", value)
  6. # バイナリ形式を16進数文字列に変換する
  7. hex_str = "".join("{:02x}".format(b) for b in packed)
  8. # 16進数文字列を2進数文字列に変換する
  9. bin_str = "".join(bin(int(ch, 16))[2:].zfill(4) for ch in hex_str)
  10. return bin_str
  11.  
  12. print(float_to_bin(0.75))
  13.  
Success #stdin #stdout 0.03s 9812KB
stdin
Standard input is empty
stdout
0011111111101000000000000000000000000000000000000000000000000000