package com.util;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
public class Util {
// char[]转byte[]
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 byte getByte (char c) {
Charset cs = Charset.forName ("UTF-8");
CharBuffer cb = CharBuffer.allocate (1);
cb.put (c);
cb.flip ();
ByteBuffer bb = cs.encode (cb);
byte [] tmp = bb.array();
return tmp[0];
}
// byte转char
public static 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();
}
// byte转char
public static char getChar(byte bytes) {
Charset cs = Charset.forName ("UTF-8");
ByteBuffer bb = ByteBuffer.allocate (1);
bb.put (bytes);
bb.flip ();
CharBuffer cb = cs.decode (bb);
char [] tmp = cb.array();
return tmp[0];
}
}
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
public class Util {
// char[]转byte[]
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 byte getByte (char c) {
Charset cs = Charset.forName ("UTF-8");
CharBuffer cb = CharBuffer.allocate (1);
cb.put (c);
cb.flip ();
ByteBuffer bb = cs.encode (cb);
byte [] tmp = bb.array();
return tmp[0];
}
// byte转char
public static 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();
}
// byte转char
public static char getChar(byte bytes) {
Charset cs = Charset.forName ("UTF-8");
ByteBuffer bb = ByteBuffer.allocate (1);
bb.put (bytes);
bb.flip ();
CharBuffer cb = cs.decode (bb);
char [] tmp = cb.array();
return tmp[0];
}
}