I'm encountering the following question. I've written some double-type data into binary files using C and now I want to read them using Python. When I used python function
with open("test.dat","rb") as dfile:
data = dfile.read()
It gave me
b'\x00\x00\x00\x00\x00\x00\xf8?\x00\x00\x00\x00\x00\x00\x04@\x00\x00\x00\x00\x00\x00\n@\x00\x00\x00\x00\x00\x00\x11@'
So I tried to decode using data.decode(), then it gave me decoding error. I suppose it was because I used the wrong encoding type. But I tried ascii and utf-8 and they did not work. Therefore my questions is 2-fold:
How can i read an binary file without knowing the encoding type?
Since i did not give an encoding type when writing the binary file in c, does c encode the data at all? If yes, what kind of encoding type would that be?
FYI, the code i used to write binary file in the first place is
#include <stdio.h>
int main(){
double buffer[4]= {1.5, 2.5, 3.25, 4.25};
FILE *ptr;
ptr = fopen("test.dat", "wb");
fwrite(buffer,sizeof(buffer),1,ptr);
printf("%ld\n",sizeof(buffer));
return 0;
}
struct.unpackin the standard library: docs.python.org/3/library/struct.htmlprintf("%zu\n",sizeof(buffer));