Hi,
When I want to convert a byte array to a String object, I can use
String constructor with charset parameter:
String result = new String(bytearray, charset);
Here, if the bytearray contains any illegal sequence of bytes in
charset, they seems to be replaced with `?'. Is it possible to throw
Exception instead of using replacement characters?
Regards,
--
Sakagami Hiroki
Timo Stamm - 02 Jul 2006 17:37 GMT
Sakagami Hiroki schrieb:
> Hi,
>
[quoted text clipped - 6 lines]
> charset, they seems to be replaced with `?'. Is it possible to throw
> Exception instead of using replacement characters?
Not using the String class. The String class uses the Charset and
CharsetDecoder classes and you can do so too:
CharsetDecoder d = Charset.forName("US-ASCII").newDecoder();
try {
CharBuffer r = d.decode(ByteBuffer.wrap(bytearray));
r.toString(); // your encoded String
} catch(CharacterCodingException e) {
// ...
}
Timo
Sakagami Hiroki - 03 Jul 2006 15:39 GMT
> Not using the String class. The String class uses the Charset and
> CharsetDecoder classes and you can do so too:
[quoted text clipped - 6 lines]
> // ...
> }
Works fine. Thanks!
--
Sakagami Hiroki