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

前端 未结 13 2161
终归单人心
终归单人心 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:24

    For decoding a series of bytes to a normal string message I finally got it working with UTF-8 encoding with this code:

    /* Convert a list of UTF-8 numbers to a normal String
     * Usefull for decoding a jms message that is delivered as a sequence of bytes instead of plain text
     */
    public String convertUtf8NumbersToString(String[] numbers){
        int length = numbers.length;
        byte[] data = new byte[length];
    
        for(int i = 0; i< length; i++){
            data[i] = Byte.parseByte(numbers[i]);
        }
        return new String(data, Charset.forName("UTF-8"));
    }
    
    0 讨论(0)
  • 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 );
    }
    
    0 讨论(0)
  • 2020-11-22 13:29
    //query is your json   
    
     DefaultHttpClient httpClient = new DefaultHttpClient();
     HttpPost postRequest = new HttpPost("http://my.site/test/v1/product/search?qy=");
    
     StringEntity input = new StringEntity(query, "UTF-8");
     input.setContentType("application/json");
     postRequest.setEntity(input);   
     HttpResponse response=response = httpClient.execute(postRequest);
    
    0 讨论(0)
  • 2020-11-22 13:29
    Charset UTF8_CHARSET = Charset.forName("UTF-8");
    String strISO = "{\"name\":\"א\"}";
    System.out.println(strISO);
    byte[] b = strISO.getBytes();
    for (byte c: b) {
        System.out.print("[" + c + "]");
    }
    String str = new String(b, UTF8_CHARSET);
    System.out.println(str);
    
    0 讨论(0)
  • 2020-11-22 13:33
    String original = "hello world";
    byte[] utf8Bytes = original.getBytes("UTF-8");
    
    0 讨论(0)
  • 2020-11-22 13:35

    As an alternative, StringUtils from Apache Commons can be used.

     byte[] bytes = {(byte) 1};
     String convertedString = StringUtils.newStringUtf8(bytes);
    

    or

     String myString = "example";
     byte[] convertedBytes = StringUtils.getBytesUtf8(myString);
    

    If you have non-standard charset, you can use getBytesUnchecked() or newString() accordingly.

    0 讨论(0)
提交回复
热议问题