Solved

Convert string with binary value to a IEEE754 Float.

  • 9 December 2020
  • 7 replies
  • 52 views

Userlevel 6
Badge +33

I try to convert the binary value

01000000 10001010 10111011 10001100

to the value 4.33.

Also see the example:

exampleReproducable on this site:

http://binaryconvert.com/result_float.html?hexadecimal=408ABB8C

Is this doable in FME? And how?

Thanks!

icon

Best answer by ebygomm 9 December 2020, 17:01

View original

7 replies

Userlevel 4

I think a few lines of Python is justified here, e.g.

import struct
 
flt = '01000000 10001010 10111011 10001100'
 
f = int(flt.replace(' ', ''), 2)  # Remove spaces
print(struct.unpack('f', struct.pack('I', f))[0])

Result:

4.335393905639648

It should be fairly easy to incorporate into a PythonCaller.

Userlevel 1
Badge +21

If you don't mind python, struct is a standard library I think

import fme
import fmeobjects
import struct
 
def processFeature(feature):
    
    binary = feature.getAttribute('value').replace(" ","")
    float = struct.unpack('f', struct.pack('I', int(binary,2)))[0]
    feature.setAttribute("float",float)

 

Userlevel 6
Badge +33

I think a few lines of Python is justified here, e.g.

import struct
 
flt = '01000000 10001010 10111011 10001100'
 
f = int(flt.replace(' ', ''), 2)  # Remove spaces
print(struct.unpack('f', struct.pack('I', f))[0])

Result:

4.335393905639648

It should be fairly easy to incorporate into a PythonCaller.

Thanks for the quick reply!

Userlevel 6
Badge +33

If you don't mind python, struct is a standard library I think

import fme
import fmeobjects
import struct
 
def processFeature(feature):
    
    binary = feature.getAttribute('value').replace(" ","")
    float = struct.unpack('f', struct.pack('I', int(binary,2)))[0]
    feature.setAttribute("float",float)

 

Thanks for the working piece! Really appreciate it.

Userlevel 6
Badge +33

If you don't mind python, struct is a standard library I think

import fme
import fmeobjects
import struct
 
def processFeature(feature):
    
    binary = feature.getAttribute('value').replace(" ","")
    float = struct.unpack('f', struct.pack('I', int(binary,2)))[0]
    feature.setAttribute("float",float)

 

Thanks again for this @ebygomm​ it works great. Providing a working example is really fantastic for people like me. I always have a really hard time to get anything Python related working, but tweaking something existing, what works, is doable. 

I believe combining FME and Python will return far better solutions then FME only.  I would love to do this more, but somehow learning it really gives me a struggle.

Note: I don't have a programming background. And I think a lot FME users are in the same boat, else they would have fixed most of their issues using Python.

Userlevel 1
Badge +21

Thanks again for this @ebygomm​ it works great. Providing a working example is really fantastic for people like me. I always have a really hard time to get anything Python related working, but tweaking something existing, what works, is doable.

I believe combining FME and Python will return far better solutions then FME only. I would love to do this more, but somehow learning it really gives me a struggle.

Note: I don't have a programming background. And I think a lot FME users are in the same boat, else they would have fixed most of their issues using Python.

My python knowledge is gained almost entirely from reading these boards. I've never used python outside of FME! I'd avoided pythoncallers completely until I had an issue that was worth the pay off in time spent v timesaved.

Userlevel 4

Thanks again for this @ebygomm​ it works great. Providing a working example is really fantastic for people like me. I always have a really hard time to get anything Python related working, but tweaking something existing, what works, is doable.

I believe combining FME and Python will return far better solutions then FME only. I would love to do this more, but somehow learning it really gives me a struggle.

Note: I don't have a programming background. And I think a lot FME users are in the same boat, else they would have fixed most of their issues using Python.

Fortunately there is an enormeous amount of Python tutorials out there, most of them free.

For a general overview of teaching resources: https://wiki.python.org/moin/BeginnersGuide/Programmers

Step-by-step tutorial for beginners: https://python3.guide/

It's well worth the investment if you regularly work with non-trivial issues, in my opinion. Both within and without FME.

Reply