How to convert Strings to and from UTF8 byte arrays in Java

前端 未结 13 2322
终归单人心
终归单人心 2020-11-22 13:05

In Java, I have a String and I want to encode it as a byte array (in UTF8, or some other encoding). Alternately, I have a byte array (in some known encoding) and I want to c

13条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 13:25

    terribly late but i just encountered this issue and this is my fix:

    private static String removeNonUtf8CompliantCharacters( final String inString ) {
        if (null == inString ) return null;
        byte[] byteArr = inString.getBytes();
        for ( int i=0; i < byteArr.length; i++ ) {
            byte ch= byteArr[i]; 
            // remove any characters outside the valid UTF-8 range as well as all control characters
            // except tabs and new lines
            if ( !( (ch > 31 && ch < 253 ) || ch == '\t' || ch == '\n' || ch == '\r') ) {
                byteArr[i]=' ';
            }
        }
        return new String( byteArr );
    }
    

提交回复
热议问题