标签:lan 个数 set while throws lang java for ati
java byte[]与十六进制字符串相互转换
import java.util.Arrays;
public class ccc {
public static void main(String[] args) {
int[] array ={-6, 1, 18, 114, 54, 0, -11, 16, 5, 3, -23, -116, -13, -24, 121, 36};
System.out.println(Arrays.toString(array));
}
}
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;
}
}
package com.anjz.test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class ByteArrayTest {
public static void main(String[] args) throws IOException {
String str = "你好";
ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes("utf-8"));
byte[] bytes = new byte[str.getBytes("utf-8").length];
bis.read(bytes);
for(byte b :bytes){
System.out.print(b+",");
}
}
}
运行结果:
-28,-67,-96,-27,-91,-67,
package com.anjz.test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class ByteArrayTest {
public static void main(String[] args) throws IOException {
String str = "你好";
ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes("utf-8"));
int temp = 0;
while((temp = bis.read())!=-1){
System.out.print(temp+",");
}
}
}
运行结果:
228,189,160,229,165,189,
案例3 与 案例4
实验一中直接输出的byte数据,实验二直接输出的是int数据,但两个数据是不一样的,我们把两个结果的数据放到一块。
-28,-67,-96,-27,-91,-67,
228,189,160,229,165,189,
发现一个规律:每列数据的绝对值加一起是个固定值256,这是一个巧合,还是一个规律?关于这个问题,首先我们看一下bis.read()的源码。
标签:lan 个数 set while throws lang java for ati
原文地址:https://www.cnblogs.com/gqv2009/p/14601163.html