diff --git a/qpython/qwriter.py b/qpython/qwriter.py index d24fa5d..89d8e6e 100644 --- a/qpython/qwriter.py +++ b/qpython/qwriter.py @@ -162,10 +162,12 @@ def _write_string(self, data): if not self._options.single_char_strings and len(data) == 1: self._write_atom(ord(data), QCHAR) else: - self._buffer.write(struct.pack('=bxi', QSTRING, len(data))) if isinstance(data, str): - self._buffer.write(data.encode(self._encoding)) + encoded_data = data.encode(self._encoding) + self._buffer.write(struct.pack('=bxi', QSTRING, len(encoded_data))) + self._buffer.write(encoded_data) else: + self._buffer.write(struct.pack('=bxi', QSTRING, len(data))) self._buffer.write(data)