主要是解码器的选择
1、char[]->byte[]
2、byte[]->char[]
1、char[]->byte[]
private byte[] getBytes (char[] chars) {
Charset cs = Charset.forName ("UTF-8");
CharBuffer cb = CharBuffer.allocate (chars.length);
cb.put (chars);
cb.flip ();
ByteBuffer bb = cs.encode (cb);
return bb.array();
}
2、byte[]->char[]
private char[] getChars (byte[] bytes) {
Charset cs = Charset.forName ("UTF-8");
ByteBuffer bb = ByteBuffer.allocate (bytes.length);
bb.put (bytes);
bb.flip ();
CharBuffer cb = cs.decode (bb);
return cb.array();
}