java byte[]与十六进制字符串相互转换
2021-06-06 18:04
标签:lan 个数 set while throws lang java for ati java byte[]与十六进制字符串相互转换 运行结果: 运行结果: 案例3 与 案例4 -28,-67,-96,-27,-91,-67, 228,189,160,229,165,189, 发现一个规律:每列数据的绝对值加一起是个固定值256,这是一个巧合,还是一个规律?关于这个问题,首先我们看一下bis.read()的源码。 java byte[]与十六进制字符串相互转换 标签:lan 个数 set while throws lang java for ati 原文地址:https://www.cnblogs.com/gqv2009/p/14601163.html案例1
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));
}
}
案例2
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
案例3
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,案例4
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,
实验一中直接输出的byte数据,实验二直接输出的是int数据,但两个数据是不一样的,我们把两个结果的数据放到一块。
上一篇:java的跨平台原理
下一篇:Python中的异常和错误
文章标题:java byte[]与十六进制字符串相互转换
文章链接:http://soscw.com/index.php/essay/91388.html