I am trying to read a binary file using following command:
import numpy as np
fid = open(filename, 'rb')
ax = np.fromfile(fid, dtype=np.int32, count=1)
This command is working fine, However
ay = np.fromfile(fid, dtype=np.char, count=16)
gives an TypeError: data type not understood. Any idea how can i read it as character type?
Your desired data type is non-existent, np.char actually is a module.
Take a look at the numpy datatypes, you could cover your byte representation using np.byte, which is a np.int8.
You should use
ay = np.fromfile(fid, dtype=np.byte, count=16)
instead of
ay = np.fromfile(fid, dtype=np.char, count=16)
because numpy doesn't contain scalar type char. More about numpy data types you could see here. numpy.byte type corresponding to C char type.
If you want convert array of 16 binary digits to one int you can use following code:
aybin = np.fromfile(fid, dtype=np.char, count=16)
ay = int(("".join(str(d) for d in aybin)), 2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With