传输的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. 使用左右移动实现
App->Ble 小端存储:
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】
使用小端存储: 低字节存储低字节 高字节存储高字节
i= 0 右移24个字节 00000000 00000000 00000000 00001100
转化为byte, 丢弃前3个字节, 00001100 高字节
transitionByte[3]=b[0]; 高字节存储高地址
transitionByte[2]:
i=1 右移动16 个字节 00000000 00000000 00001100 00001111
转化为byte, 丢弃前3个字节 00001111
最终:00001111
Ble->App 数据解析
byte[] sTime = new byte[4]; // 小端存储 byte[0] 存储低位
System.arraycopy(payload, 3, sTime, 0, sTime.length); // 接收数据保存到byte数值中
public static long bytes2Int(byte[] bt) {
byte[] b = new byte[4];
b[3] = bt[0];
b[2] = bt[1];
b[1] = bt[2];
b[0] = bt[3];
long result = 0;
// b[0] 高位
// i=0 , << 左移 24 位 , & 0xFF000000 获取高位的值
// result = result | tmpVal; 获取高位的值 保存long 中
for (int i = 0; i < b.length; i++) {
int tmpVal = (b[i] << (8 * (3 - i)));
switch (i) {
case 0:
tmpVal = tmpVal & 0xFF000000;
break;
case 1:
tmpVal = tmpVal & 0x00FF0000;
break;
case 2:
tmpVal = tmpVal & 0x0000FF00;
break;
case 3:
tmpVal = tmpVal & 0x000000FF;
break;
}
result = result | tmpVal;
}
return result;
}
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;
}
}