Reputation: 9
I did use matlab for bin format data analysis. in matlab, just typing below command can read and convert bin file to int16 format.
fid=fopen(fname,'rb');
data = fread(fid,'int16');
data =
749
774
798
823
846
869
892 ...
this is desirable form. but in python, i did try to read and convert bin file to data format, value becomes
f = open('data.bin', 'rb')
data = f.read()
data
b'\xed\x02\x06\x03\x1e\x037\x03N\x03e\x03|\x03\x92\x03\xa9\x03\xbc\x03\xcd\x03\xdf\x03\xe9\x03\xf2\x03\xee\x03\xe9\x03\xde\x03\xca\x03\xbb\x03\xae\x03\xa4 ...
int_val = int.from_bytes(byte_val, "big")
int_val
506245810864297737878221011897654703022162227130237119582700991733805191065310841283412291727786916
this format. is there any idea about this?
Upvotes: 0
Views: 2014
Reputation: 177715
Using the built-in array
module and typecode 'H'
for unsigned 16-bit data:
import array
byte_data = b'\xed\x02\x06\x03\x1e\x037\x03N\x03e\x03|\x03\x92\x03\xa9\x03\xbc\x03\xcd\x03\xdf\x03\xe9\x03\xf2\x03\xee\x03\xe9\x03\xde\x03\xca\x03\xbb\x03\xae\x03'
data = array.array('H',byte_data)
print(data)
Output:
array('H', [749, 774, 798, 823, 846, 869, 892, 914, 937, 956, 973, 991, 1001, 1010, 1006, 1001, 990, 970, 955, 942])
Or if you prefer a Python list
, just use:
data = list(array.array('H',byte_data))
Upvotes: 1