‘float.hex()’ method is used to convert a floating point number to its hexadecimal value. Similarly, we can use ‘float.fromhex()’ method to convert a hexadecimal string value to its floating point representation. ‘hex()’ is an instance method but ‘fromhex()’ is a class method.
Below is the code which will help you..
#define numpy array
np_arr = np.array([1.2,3.4,2.6,2.1,15,10], dtype = np.float32)
#convert numpy array to hex
np_arr_hex = np.array([float.hex(float(x)) for x in np_arr])
#back to float with upto 4 decimal places
np_arr_float = np.array([round(float.fromhex(x),1) for x in np_arr_hex])
#print both arrays
np_arr_hex,np_arr_float
Output:
np_arr_hex
(array(['0x1.3333340000000p+0', '0x1.b333340000000p+1',
'0x1.4ccccc0000000p+1', '0x1.0ccccc0000000p+1',
'0x1.e000000000000p+3', '0x1.4000000000000p+3'], dtype='<U20')
np_arr_float
array([ 1.2, 3.4, 2.6, 2.1, 15. , 10. ]))