传输的2种方式:
1. 16 进制传输, 可以打印字符, 所有的内容都是0x00-0xFF,转化为byte的时候直接把16进制转化为byte
2. AscII 转化 new String(byte), 比如特殊字符串abc , 转化为byte的时候取字符的ASCII码,转化为byte
1. 首先理解左移、右移:
1<<1 =2 : 1左移1位, 相当于乘2,右移多少位,乘多少个2
1: 0000 0001 1
<<1: 0000 0010 2,超出8位长度丢掉,左边补0
2>>1 = 1 : 右移, 右边补0
2: 0000 0010 2
2>>1 0000 0001 1
如何把int 转化为byte 数组
使用byteArrayOutputStream
使用左移、右移
2. 使用左右移动实现
看程序:
public static byte[] int2BytesArray(int n) {
byte[] b = new byte[4];
for (int i = 0; i < 4; i++) {
b[i] = (byte) (n >> (24 - i * 8));
}
byte[] transitionByte=new byte[4];
transitionByte[3]=b[0];
transitionByte[2]=b[1];
transitionByte[1]=b[2];
transitionByte[0]=b[3];
return transitionByte;
}
比如: 00001100 00001111 00001001 00000001 : 202311937【int】
使用小端存储:
transitionByte[0]:
内存低地址,存储高字节,00001100,如何获取高字节00001100, >>右移0个字节 00001100 00001111 00001001 00000001
转化为byte, 丢弃前3个字节
最终:00000001
transitionByte[1]:
如何获取高字节1111, >>右移8个字节 00000000 00001100 00001111 00001001
转化为byte, 丢弃前3个字节
最终:00001001
transitionByte[2], >>右移16个字节 00000000 00000000 00001100 00001111
转化为byte, 丢弃前3个字节
最终:00001111
transitionByte[3], >>右移24个字节 00000000 00000000 00000000 00001100
转化为byte, 丢弃前3个字节
最终:00001100
int v = src[i] & 0xFF; // byte -> int, 调试的时候控制台默认转化
String hv = Integer.toHexString(v); int 转化为 hex
int byteint = Integer.parseInt(swap, 16) & 0xFF; // 16进制转化为int
b[j] = new Integer(byteint).byteValue(); // int 转化为byte
3. 用例程序:
package com.denganzhi.test;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class Main2 {
public static void main(String[] args) throws IOException {
System.out.println(1<<2);
byte[] srcByte= int2BytesArray(202311937);
// 对应二机制: 00001100 00001111 00001001 00000001
// hex: 0c 0f 09 01
//1. 方式1 小端存储输出: 01090f0c
// 01高字节,存储低内存 01 byte[0]
// 0c低字节,存储在高内存0c byte[3]
System.out.println(bytesToHexString(srcByte));
ByteArrayOutputStream bos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(bos);
dos.writeInt(202311937);
dos.flush();
byte[] srcOut= bos.toByteArray();
// 输出是: 0c0f0901 eclipse编辑器默认是大端存储输出 Windows默认
// 2. 方式2
System.out.println(bytesToHexString(srcOut));
}
// 小端存储
public static byte[] int2BytesArray(int n) {
byte[] b = new byte[4];
for (int i = 0; i < 4; i++) {
b[i] = (byte) (n >> (24 - i * 8));
}
byte[] transitionByte=new byte[4];
transitionByte[3]=b[0];
transitionByte[2]=b[1];
transitionByte[1]=b[2];
transitionByte[0]=b[3];
return transitionByte;
}
public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
// 如何把byte转化为 hex 16进制输出
// byte - >int -> hex
public static String byteToHexString(byte src) {
StringBuilder stringBuilder = new StringBuilder("");
int v = src & 0xFF; //这里为什么可以转化为int,默认是int
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
return stringBuilder.toString();
}
// 16进制-》 转化为char[]逐个取出来-》 int -> byte ->拼接
public static final byte[] hex2byte(String hex)
throws IllegalArgumentException {
if (hex.length() % 2 != 0) {
throw new IllegalArgumentException();
}
char[] arr = hex.toCharArray();
byte[] b = new byte[hex.length() / 2];
for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) {
String swap = "" + arr[i++] + arr[i];
int byteint = Integer.parseInt(swap, 16) & 0xFF;
b[j] = new Integer(byteint).byteValue();
}
return b;
}
}