I have a Python project where I have a fixed byte-length text field (NOT FIXED CHAR-LENGTH FIELD) in a comm protocol that contains a utf-8 encoded, NULL padded, NULL terminated string.
I need to ensure that a string fits into the fixed byte-length field. Since utf-8 is a variable width encoding, this makes using brute force to truncate the string at a fixed byte length dicey since you could possibly leave part of a multi-byte character dangling at the end.
Is there a module/method/function/etc that can help me with truncating utf-8 variable width encoded strings to a fixed byte-length?
Something that does Null padding and termination would be a bonus.
This seems like a nut that would have already been cracked. I don't want to reinvent something if it already exists.