import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.*;
/**
*
* @ClassName: ByteToChar
* @Description: 主要作用是将字符与字节的转换
* @author ZXD
* @date 2017年8月30日 上午10:07:18
* @version 1.0.0
*/
public class ByteToChar {
public 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();
}
public 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();
}
}