最近写了一个GPS设备TCP网关服务,第一次这么密积的用到上学时计算原理里的内容,还是转为hex或0101字符串好理解,下面是用到二进制工具类
部分来自,感谢:https://gitee.com/liu_yang0323/jt808-tcp-netty
import cn.hutool.core.codec.BCD;
import lombok.extern.slf4j.Slf4j;
import javax.xml.bind.DatatypeConverter;
import java.util.Arrays;
import java.util.List;
@Slf4j
public class BytesUtil {
//hex过的字符串转换为bytes
public static byte[] hexStr2ByteArray(String hexStr){
return DatatypeConverter.parseHexBinary(hexStr);
}
public static String byteArray2HexStr(byte[] bytes){
return DatatypeConverter.printHexBinary(bytes);
}
//字节串是否相等
public static boolean bytesEquals(byte[] b1,byte[] b2){
return Arrays.equals(b1,b2);
}
//取子字节串
public static byte[] getSubBytes(byte[] bytes,int start,int size){
if(bytes.length < start + size) throw new IllegalArgumentException("bytes size error");
var result = new byte[size];
for (int i = 0; i < bytes.length; i++) {
if(i>=start && i<start+size){
result[i-start] = bytes[i];
}
}
return result;
}
//byte转换为八位二进制字行串10110111形式
public static String byte2BinStr(byte b){
return Integer.toBinaryString((b & 0xFF) + 0x100).substring(1);
}
//获取bytes中某一位是0还是1,从右到左,从0计
public static String getBitValueFromByteArray(byte[] bytes,int bitNum){
var binLength = bytes.length * 8;
if(bitNum> binLength -1)
throw new IllegalArgumentException("bitNum out of bytes");
StringBuilder stringBuffer = new StringBuilder();
for (byte aByte : bytes) {
stringBuffer.append(byte2BinStr(aByte));
}
String binStr = stringBuffer.toString();
return binStr.substring(binLength-bitNum-1,binLength-bitNum);
}
/**
* 把一个整形该为byte
*
* @param value
* @return
* @throws Exception
*/
public static byte integerTo1Byte(int value) {
return (byte) (value & 0xFF);
}
/**
* 把一个整形该为1位的byte数组
*
* @param value
* @return
* @throws Exception
*/
public static byte[] integerTo1Bytes(int value) {
byte[] result = new byte[1];
result[0] = (byte) (value & 0xFF);
return result;
}
/**
* 把一个整形改为2位的byte数组
*
* @param value
* @return
* @throws Exception
*/
public static byte[] integerTo2Bytes(int value) {
byte[] result = new byte[2];
result[0] = (byte) ((value >>> 8) & 0xFF);
result[1] = (byte) (value & 0xFF);
return result;
}
/**
* 把一个整形改为3位的byte数组
*
* @param value
* @return
* @throws Exception
*/
public static byte[] integerTo3Bytes(int value) {
byte[] result = new byte[3];
result[0] = (byte) ((value >>> 16) & 0xFF);
result[1] = (byte) ((value >>> 8) & 0xFF);
result[2] = (byte) (value & 0xFF);
return result;
}
/**
* 把一个整形改为4位的byte数组
*
* @param value
* @return
* @throws Exception
*/
public static byte[] integerTo4Bytes(int value){
byte[] result = new byte[4];
result[0] = (byte) ((value >>> 24) & 0xFF);
result[1] = (byte) ((value >>> 16) & 0xFF);
result[2] = (byte) ((value >>> 8) & 0xFF);
result[3] = (byte) (value & 0xFF);
return result;
}
/**
* 把byte[]转化位整形,通常为指令用
*
* @param value
* @return
* @throws Exception
*/
public static int byteToInteger(byte[] value) {
int result;
if (value.length == 1) {
result = oneByteToInteger(value[0]);
} else if (value.length == 2) {
result = twoBytesToInteger(value);
} else if (value.length == 3) {
result = threeBytesToInteger(value);
} else if (value.length == 4) {
result = fourBytesToInteger(value);
} else {
result = fourBytesToInteger(value);
}
return result;
}
public static String byteToBcdString(byte[] bytes) {
return BCD.bcdToStr(bytes);
}
/**
* 字符串==>BCD字节数组
*
* @param str
* @return BCD字节数组
*/
public static byte[] bcdStr2ByteArray(String str) {
return BCD.strToBcd(str);
}
/**
* 把一个byte转化位整形,通常为指令用
*
* @param value
* @return
* @throws Exception
*/
public static int oneByteToInteger(byte value) {
return (int) value & 0xFF;
}
/**
* 把一个2位的数组转化位整形
*
* @param value
* @return
* @throws Exception
*/
public static int twoBytesToInteger(byte[] value) {
// if (value.length < 2) {
// throw new Exception("Byte array too short!");
// }
int temp0 = value[0] & 0xFF;
int temp1 = value[1] & 0xFF;
return ((temp0 << 8) + temp1);
}
/**
* 把一个3位的数组转化位整形
*
* @param value
* @return
* @throws Exception
*/
public static int threeBytesToInteger(byte[] value) {
int temp0 = value[0] & 0xFF;
int temp1 = value[1] & 0xFF;
int temp2 = value[2] & 0xFF;
return ((temp0 << 16) + (temp1 << 8) + temp2);
}
/**
* 把一个4位的数组转化位整形,通常为指令用
*
* @param value
* @return
* @throws Exception
*/
public static int fourBytesToInteger(byte[] value) {
// if (value.length < 4) {
// throw new Exception("Byte array too short!");
// }
int temp0 = value[0] & 0xFF;
int temp1 = value[1] & 0xFF;
int temp2 = value[2] & 0xFF;
int temp3 = value[3] & 0xFF;
return ((temp0 << 24) + (temp1 << 16) + (temp2 << 8) + temp3);
}
/**
* 把一个4位的数组转化位整形
*
* @param value
* @return
* @throws Exception
*/
public static long fourBytesToLong(byte[] value) throws Exception {
// if (value.length < 4) {
// throw new Exception("Byte array too short!");
// }
int temp0 = value[0] & 0xFF;
int temp1 = value[1] & 0xFF;
int temp2 = value[2] & 0xFF;
int temp3 = value[3] & 0xFF;
return (((long) temp0 << 24) + (temp1 << 16) + (temp2 << 8) + temp3);
}
/**
* 把一个数组转化长整形
*
* @param value
* @return
* @throws Exception
*/
public static long bytes2Long(byte[] value) {
long result = 0;
int len = value.length;
int temp;
for (int i = 0; i < len; i++) {
temp = (len - 1 - i) * 8;
if (temp == 0) {
result += (value[i] & 0x0ff);
} else {
result += (value[i] & 0x0ff) << temp;
}
}
return result;
}
/**
* 把一个长整形改为byte数组
*
* @param value
* @return
* @throws Exception
*/
public static byte[] longToBytes(long value){
return longToBytes(value, 8);
}
/**
* 把一个长整形改为byte数组
*
* @param value
* @return
* @throws Exception
*/
public static byte[] longToBytes(long value, int len) {
byte[] result = new byte[len];
int temp;
for (int i = 0; i < len; i++) {
temp = (len - 1 - i) * 8;
if (temp == 0) {
result[i] += (value & 0x0ff);
} else {
result[i] += (value >>> temp) & 0x0ff;
}
}
return result;
}
/**
* 把IP拆分位int数组
*
* @param ip
* @return
* @throws Exception
*/
public static int[] getIntIPValue(String ip) throws Exception {
String[] sip = ip.split("[.]");
// if (sip.length != 4) {
// throw new Exception("error IPAddress");
// }
int[] intIP = { Integer.parseInt(sip[0]), Integer.parseInt(sip[1]), Integer.parseInt(sip[2]),
Integer.parseInt(sip[3]) };
return intIP;
}
/**
* 把byte类型IP地址转化位字符串
*
* @param address
* @return
* @throws Exception
*/
public static String getStringIPValue(byte[] address) throws Exception {
int first = oneByteToInteger(address[0]);
int second = oneByteToInteger(address[1]);
int third = oneByteToInteger(address[2]);
int fourth = oneByteToInteger(address[3]);
return first + "." + second + "." + third + "." + fourth;
}
/**
* 合并字节数组
*
* @param first
* @param rest
* @return
*/
public static byte[] concatAll(byte[] first, byte[]... rest) {
int totalLength = first.length;
for (byte[] array : rest) {
if (array != null) {
totalLength += array.length;
}
}
byte[] result = Arrays.copyOf(first, totalLength);
int offset = first.length;
for (byte[] array : rest) {
if (array != null) {
System.arraycopy(array, 0, result, offset, array.length);
offset += array.length;
}
}
return result;
}
/**
* 合并字节数组
*
* @param rest
* @return
*/
public static byte[] concatAll(List<byte[]> rest) {
int totalLength = 0;
for (byte[] array : rest) {
if (array != null) {
totalLength += array.length;
}
}
byte[] result = new byte[totalLength];
int offset = 0;
for (byte[] array : rest) {
if (array != null) {
System.arraycopy(array, 0, result, offset, array.length);
offset += array.length;
}
}
return result;
}
`//校验和算法
public static int getCheckSum(byte[] bytes){
int cs = 0;
for (int i = 0; i < bytes.length; i++) {
cs = cs ^ bytes[i];
}
return cs;
}
public static String gbk2String(byte[] bytes){
try {
return new String(bytes, "GBK");
}catch (Exception e){
log.error("gbk2String error,bytes:{}",byteArray2HexStr(bytes));
}
return "";
}
public static void main(String [] arg){
//System.out.println(byteArray2HexStr(integerTo1Bytes(getCheckSum(hexStr2ByteArray("810000090133057845773E95000000313233343536")))));
}
}