Skip to main content

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!

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))k0])

Result:

4.335393905639648

It should be fairly easy to incorporate into a PythonCaller.


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)

 


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))k0])

Result:

4.335393905639648

It should be fairly easy to incorporate into a PythonCaller.

Thanks for the quick reply!


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.


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.


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.


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