protected static String correctToUtf8 ( String inputString ) { final StringBuilder result = new StringBuilder ( inputString . length ( ) ) ; final StringCharacterIterator it = new StringCharacterIterator ( inputString ) ; char ch = it . current ( ) ; boolean modified = false ; while ( ch != CharacterIterator . DONE ) { if ( Character . isISOControl ( ch ) ) { if ( ch == ' ' || ch == ' ' ) { result . append ( ch ) ; } else { modified = true ; result . append ( String . format ( "0x%x" , ( int ) ch ) ) ; } } else { result . append ( ch ) ; } ch = it . next ( ) ; } if ( ! modified ) return inputString ; return result . toString ( ) ; }
Make sure the string contains valid UTF - 8 characters