Jump to content
  • Advertisement
Sign in to follow this  
3DModelerMan

Writing python strings for C++

This topic is 1341 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement

You need to convert the string to a bytes object before you serialize it. However, once it's converted to a bytes object you don't need to bother sending it through struct, just write it to the file.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!