0

I am trying to save some data obtained in Python to a binary file that one could read using C. The problem is that strings in Python and C are encoded differently, which creates unnecessary mess while reading the file.

Create a file using Python:

from array import array

string = "test"
text_array = array('u', list(string))

output_file = open('test_file.bin', 'wb')
text_array.tofile(output_file)
output_file.close()

Read the same file using C:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *file_pointer = fopen("test_file.bin", "rb");
    char *symbol;
    fread(symbol, sizeof(char), 16, file_pointer);
    char * text = (char * ) malloc( 4 * sizeof(char) );
    for(int j = 0; j < 4; j++) text[j] = symbol[4 * j];
    printf("%s \n", text);
    fclose(file_pointer);
    return 0;
}

Ideally, I would like to save the text "test" using Python in a way that it takes 4 bytes, not 16 bytes, and so the C-code for reading would be cleaner.

I have tried to convert Unicode to ANSII in Python, but it is not clear how to proceed further with writing.

Barmar
  • 741,623
  • 53
  • 500
  • 612

0 Answers0