package gj;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;
/**
* Author: areful
* Date: 2019/3/28
*/
public class CharByteKit {
public static 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 static char[] getChars(byte[] bytes) {
Charset cs = Charset.forName("UTF-8");
ByteBuffer bb = ByteBuffer.allocate(bytes.length);
bb.put(bytes).flip();
CharBuffer cb = cs.decode(bb);
return cb.array();
}
public static byte[] charToByte(char c) {
byte[] b = new byte[2];
b[0] = (byte)
Java中byte[]和char[]互相转换
最新推荐文章于 2024-06-14 10:55:26 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)