Python Forum
Convert string to a specific number of bytes - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Convert string to a specific number of bytes (/thread-17099.html)



Convert string to a specific number of bytes - artblinked - Mar-28-2019

Hello!

I have a class with instance attributes. I need to create an object with each instance having its own values (some are ints, others are strings). Each instance value needs to be converted to binary and saved to a file that will be accessed and updated. My problem is saving the attributes with string values to a specific number of bytes. Each attribute value needs to be a specific number of bytes. I think I know how to convert the integer values but I can't find anything to help with the strings. I can get how large each string is in bytes but I need to save it to a specific byte size, no matter what size it actually is. Is there a way to pad it or something?

Thanks in advance,


RE: Convert string to a specific number of bytes - Larz60+ - Mar-28-2019

What have you tried?
Show your code attempts