How to convert Decimal Floating-Point To 64-bit Hexadecimal using IEEE-754 Floating-Point convention
Question: Are there any solutions to convert Decimal Floating-Point To 64-bit
Hexadecimal using IEEE-754 Floating-Point convention?

I have decimal 4275451536 which needs to be converted into IEEE-754
Hexadecimal which is 41EFDAC6D2000000

Here it is an [online
converter](http://babbage.cs.qc.cuny.edu/IEEE-754.old/Decimal.html), but I
need to write it in programming language.

How to do this using Python ?


Answer: If I understand your question correctly, you want to find the underlying
binary representation of an IEEE 754 binary64-format floating-point number,
and show that binary representation in hexadecimal.

For obtaining the underlying bits, the
[struct](https://docs.python.org/3.4/library/struct.html) module is your
friend. The
[`struct.pack`](https://docs.python.org/3.4/library/struct.html#struct.pack)
operation lets you interpret the 8 raw bytes underlying the binary64
representation as a (byte)string of length 8, and the
[`struct.unpack`](https://docs.python.org/3.4/library/struct.html#struct.unpack)
operation will then let you re-interpret that string as a nonnegative integer
(for example). Once you've got the integer, it's easy to find the hex
representation. Here's a complete example for your data:

    
    
    >>> import struct
    >>> x = 4275451536.0
    >>> bytes_of_x = struct.pack('<d', x)
    >>> bytes_of_x
    '\x00\x00\x00\xd2\xc6\xda\xefA'
    >>> x_as_int = struct.unpack('<Q', bytes_of_x)[0]
    >>> x_as_int
    4751256679360757760
    >>> hex(x_as_int)
    '0x41efdac6d2000000'
    

It's not clear from your question whether you're starting with an actual
decimal string, or a Python `float`; the example above starts with a `float`.
If your input data takes the form of a decimal string, you'll want to convert
it to float first:

    
    
    >>> my_input = "4275451536"
    >>> x = float(my_input)
    >>> # ... rest of the code as before
    

Or all in one line, and using string formatting instead of the `hex` builtin
to convert to a hexadecimal string:

    
    
    >>> '{:016x}'.format(struct.unpack('<Q', struct.pack('<d', float("4275451536")))[0])
    '41efdac6d2000000'
    

