0

I have some software which takes as input the following format (which is doing it for MATLAB):

[typecast(single(doubleNumber),'uint8') 0]

where doubleNumber is an arbitrary floating number. How can I produce this data with Python 2.7? My main problem is that I do not know what typecast does internally.

0

1 Answer 1

0

Convert the single float number into a binary string - see Binary representation of float in Python (bits not hex). Let's assume we are given with the float Test:

import struct
def binary(num):
    return ''.join(bin(ord(c)).replace('0b', '').rjust(8, '0') for c in struct.pack('!f', num))

Test = .25
binTest = binary(Test)

After that I have to separate all 8 characters and convert them into an unsigned integer - see Convert base-2 binary number string to int:

bin1 = binTest[ 0: 8]
bin2 = binTest[ 8:16]
bin3 = binTest[16:24]
bin4 = binTest[24:32]
TypecaseOfTest = [int(bin4,2),int(bin3,2),int(bin2,2),int(bin1,2),0]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.