@staticmethod
def truncate_string(value: str, max_bytes: int, encoding: str) ->bytes:
    """
        Truncates a string value to the specified number of bytes.

        :return:
            Returns bytes, truncated to the correct length.

            Note: Might be a bit shorter than `max_bytes`, to avoid
            orphaning a multibyte sequence.
        """
    bytes_ = bytearray(value.encode(encoding))
    if encoding.lower() in ['utf-8', 'utf8']:
        truncated = bytes_[0:max_bytes]
        for i, o in enumerate(reversed(truncated), start=1):
            if i < 2 and o < 128:
                break
            if o >= 192:
                seq_length = 4 if o >= 240 else 3 if o >= 224 else 2
                if seq_length != i:
                    truncated = truncated[0:-i]
                break
        return truncated
    else:
        trim = 0
        while True:
            truncated = bytes_[0:max_bytes - trim]
            try:
                truncated.decode(encoding)
            except UnicodeDecodeError:
                trim += 1
            else:
                return bytes(truncated)
            if trim >= max_bytes:
                raise ValueError(
                    'Unable to truncate {bytes_!r} to {max_bytes} bytes when encoded using {encoding}.'
                    .format(bytes_=bytes_, max_bytes=max_bytes, encoding=
                    encoding))
