1078-设置路线

This commit is contained in:
648540858
2024-05-06 15:55:14 +08:00
parent cc91db30c5
commit c53e4f6fbc
4 changed files with 71 additions and 22 deletions

View File

@@ -2,6 +2,7 @@ package com.genersoft.iot.vmp.jt1078.util;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.springframework.security.crypto.codec.Hex;
import java.time.Instant;
import java.util.Calendar;
@@ -27,6 +28,48 @@ public class BCDUtil {
return stringBuffer.toString();
}
/**
* 字符串转BCD码
* 来自: https://www.cnblogs.com/ranandrun/p/BCD.html
* @param asc ASCII字符串
* @return BCD
*/
public static byte[] strToBcd(String asc) {
int len = asc.length();
int mod = len % 2;
if (mod != 0) {
asc = "0" + asc;
len = asc.length();
}
byte abt[] = new byte[len];
if (len >= 2) {
len >>= 1;
}
byte bbt[] = new byte[len];
abt = asc.getBytes();
int j, k;
for (int p = 0; p < asc.length() / 2; p++) {
if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
j = abt[2 * p] - '0';
} else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
j = abt[2 * p] - 'a' + 0x0a;
} else {
j = abt[2 * p] - 'A' + 0x0a;
}
if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
k = abt[2 * p + 1] - '0';
} else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
k = abt[2 * p + 1] - 'a' + 0x0a;
} else {
k = abt[2 * p + 1] - 'A' + 0x0a;
}
int a = (j << 4) + k;
byte b = (byte) a;
bbt[p] = b;
}
return bbt;
}
/**
* 时间使用按照YY-MM-DD-hh-mm-ss转换为byte数据
*/
@@ -40,6 +83,12 @@ public class BCDUtil {
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
System.out.println("year== " + year);
System.out.println("month== " + month);
System.out.println("day== " + day);
System.out.println("hour== " + hour);
System.out.println("minute== " + minute);
System.out.println("second== " + second);
byteBuf.writeByte(year);
byteBuf.writeByte(month);
byteBuf.writeByte(day);