I'm trying to write a function that will write a string in Python to the format used by my engine. When I serialize strings in C++, I save the length first, then all the characters ignoring the null terminator. I'd like to be able to save from python in the same format. The problem is that struct doesn't seem to like the characters I'm giving it. I've tried using ord() but it doesn't work that way either. No matter what I do, it complains that it needs a byte of length 1.
def writeString(file, str):
file.write(struct.pack('I', len(str)));
for letter in str:
file.write(struct.pack('c', letter));
How can I convert the string characters into a C++ compatible char and save them in Python?