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.

strpeter
  • 2,562
  • 3
  • 27
  • 48

1 Answers1

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]
Community
  • 1
  • 1
strpeter
  • 2,562
  • 3
  • 27
  • 48