-3

In C, I can easily (hardest part was lX vs X) determine the internal representation of a 64 bit float, which is easy to understand and correct--HOW CAN I DO THIS IN PYTHON (2 preferred)?:

Mac_3.2.57$cat f.c
#include <stdio.h>
main(void){
    union A64bFloatOrLong{
        double F;
        long L;}a64bFloatOrLong;
    a64bFloatOrLong.F=1.0;
    printf("0x%016.16lX\n",a64bFloatOrLong.L);}
Mac_3.2.57$cc f.c
f.c:2:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main(void){
^
1 warning generated.
Mac_3.2.57$./a.out
0x3FF0000000000000
Andrew
  • 1
  • 4
  • 19
  • 3
    [How to convert a float into hex](https://stackoverflow.com/a/23624284) – 001 Aug 04 '21 at 13:30
  • 4
    Does this answer your question? [How to convert a float into hex](https://stackoverflow.com/questions/23624212/how-to-convert-a-float-into-hex) – phuclv Aug 04 '21 at 14:56
  • Yes, thank you both! (I'll need to get better at searching apparently; sorry.) :-) – Andrew Aug 04 '21 at 15:05

1 Answers1

0

As mentioned in the comments--this was previously solved, my verification copied here for convenience/posterity:

Mac_3.2.57$python
Python 2.7.10 (default, Jul 15 2017, 17:16:57) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> def double_to_hex(f):
...     return hex(struct.unpack('<Q', struct.pack('<d', f))[0])
... 
>>> double_to_hex(17.5)   # Output: '0x4031800000000000L'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in double_to_hex
NameError: global name 'struct' is not defined
>>> import struct
>>> double_to_hex(17.5)   # Output: '0x4031800000000000L'
'0x4031800000000000'
>>> double_to_hex(1.0)   # Output: '0x4031800000000000L'
'0x3ff0000000000000'
Andrew
  • 1
  • 4
  • 19