import struct


def string(data):
    """
    Converts a Python string into bytes, ready to be written to a file.

    If the string's byte representation (UTF-8) has fewer than 255 bytes, it
    is preceded by a 1-byte length; otherwise, it is preceded by ``b'\xff'`` and a
    4-byte length.
    """
    bytestring = data.encode(errors="surrogateescape")
    length = len(bytestring)
    if length < 255:
        return struct.pack(">B%ds" % length, length, bytestring)
    else:
        return struct.pack(">BI%ds" % length, 255, length, bytestring)