Sunday, May 1, 2011

how to convert tis-620 string to utf-8 string in java

how to convert tis-620 string to utf-8 string in java

From stackoverflow
  • import java.nio.ByteBuffer
    import java.nio.CharBuffer
    

    ....

    public static ByteBuffer toByteBuffer(String content, String encode) {  
          Charset charset = Charset.forName(encode);  
          ByteBuffer bb = charset.encode(content);  
           return bb;  
       }
    

    Pass as encode argument "UTF-8"

    : still not working out quite right !!!
  • private byte[] convertTis620ToUTF8(byte[] encoded)
    {
        try
        {
            String theString = new String(encoded, "TIS620");
            return theString.getBytes("UTF-8");
        } 
        catch(UnsupportedEncodingException uee)
        {
            /* Didn't work out */
        }
    }
    
    ...
    
    byte[] utf8 = convertTis620ToUTF8(tis620);
    

    Also, you might need to put charsets.jar on your classpath to support the TIS620 encoding.

0 comments:

Post a Comment