Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

problem of using Charset converting from big5 to utf8

807591Apr 27 2008 — edited Apr 27 2008
Hi all,

I may not get clear of using Charset correctly. Below is my code that trying convert a big5 String into utf-8 String. But it fails on using utf-8 decoder to decode big5 byte buffer. Does any can tell me the correct usage of using Charset to convert string from one encoding to another, especially asian character.

Below is the exception stack I got
java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(CoderResult.java:260)
at java.nio.charset.CharsetDecoder.decode(CharsetDecoder.java:771)
Charset charsetBig5 = Charset.forName("Big5");
Charset charsetUtf8 = Charset.forName("UTF-8");

String big5 = "這是中文";
CharsetEncoder big5Encoder = charsetBig5.newEncoder();
CharBuffer big5cb = CharBuffer.wrap(big5.toString());
ByteBuffer big5bb = big5Encoder.encode(big5cb);
		
CharsetDecoder utfDecoder = charsetUtf8.newDecoder();
CharBuffer utf8cb = utfDecoder.decode(big5bb);  //crashed here in runtime
		
System.out.println(utf8cb.toString());
thanks a lot
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 25 2008
Added on Apr 27 2008
2 comments
1,739 views