private static function convertString($string, $outputEncoding)
    {
        if ($string instanceof StringBuilder) {
            $inputEncoding = $string->getEncoding();
        } else {
            $inputEncoding = mb_detect_encoding((string) $string);
        }
        $string = (string) $string;
        if ($inputEncoding != $outputEncoding) {
            $string = iconv($inputEncoding, $outputEncoding, $string);
        }
        return $string;
    }