Open In App

Convert hex string to float in Python

Last Updated : 19 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Converting a hex string to a float in Python involves a few steps since Python does not have a direct method to convert a hexadecimal string representing a float directly to a float. Typically, a hexadecimal string is first converted to its binary representation, and then this binary representation is interpreted as a floating-point number.

Convert hex string to float in Python

There are different ways in Python to convert a hex string to a floating point.

Here is a step-by-step guide on how to achieve this:

Using the module struct

Python has a struct module that provides functions that can convert between values in Python and C structs represented as Python bytes objects. It is beneficial when working with binary data from network connections or files.

Example:

Start by deleting any leading or trailing spaces and removing the ‘0x’ prefix if it is present in the hex string. After this, turns the hex string into a byte array using bytes.fromhex(). Lastly, by looking at the byte array as float with format If then returning floating-point number which will make use of struct.unpack().

Python
import struct

def hex_to_float_struct(hex_str):
    hex_str = hex_str.strip().replace("0x", "")
    byte_array = bytes.fromhex(hex_str)
    float_num = struct.unpack('!f', byte_array)[0]
    return float_num

# Example
hex_str = "40490fdb"
print(hex_to_float_struct(hex_str))  

Output:

3.1415927410125732

Using binascii

Another module within the python library is binascii and it offers various methods for representing binary data of ASCII encoded strings. The procedure includes converting hexadecimal representation of a string to binary then interpreting this binary as a float.

Example:

By running these hex strings through binascii.unhexlify() function first into a byte array and finally calling struct.unpack() with format !f before returning them back together with other numbers.

Python
import binascii
import struct

def hex_to_float_binascii(hex_str):
    byte_array = binascii.unhexlify(hex_str)
    float_num = struct.unpack('!f', byte_array)[0]
    return float_num

# Example
hex_str = "40490fdb"
print(hex_to_float_binascii(hex_str))  

Output

3.1415927410125732

Using Raw Byte Manipulation

The method begins by converting the hex string to bytes and then to floating-point without any high level modules like struct.

Example:

This code converts the hexadecimal string into an integer of 16 bases it does this by using the int() function. It also changes the number to byte array with top-down approach consisting of four bytes. Finally, struct.unpack() which uses format !f is used in order to unpack byte array for creation of floating point number.

Python
import struct

def hex_to_float_manual(hex_str):
    int_value = int(hex_str, 16)
    byte_array = int_value.to_bytes(4, byteorder='big')
    float_num = struct.unpack('!f', byte_array)[0]
    return float_num

# Example 
hex_str = "40490fdb"
print(hex_to_float_manual(hex_str))  

Output:

3.1415927410125732

Conclusion

Each method has its own benefits depending on what they are trying to achieve in a given context. Going through these steps will help you effectively convert Hex string to Float when programming with Python.


Next Article
Article Tags :
Practice Tags :

Similar Reads