67,513
社区成员
发帖
与我相关
我的任务
分享
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
public class mytest {
public static void main(String[] args) {
// TODO Auto-generated method stub
byte[] bytes = new byte[]{-76, -1, 32, 30, 36};
char[] chars = getChars(bytes);
byte[] bytes2= getBytes(chars);
char[] chars2 = getChars(bytes2);
}
/** char转byte */
public static byte[] getBytes(char[] chars) {
int len = chars.length;
byte[] bytes = new byte[len];
for(int i=0;i<len;i++){
bytes[i]= (byte)(chars[i]);
}
return bytes;
}
/** byte转char */
public static char[] getChars(byte[] bytes) {
int len = bytes.length;
char[] chars = new char[len];
for(int i=0;i<len;i++){
chars[i]= (char)(bytes[i] & 0xff);
}
return chars;
}
}