update 电单车协议

This commit is contained in:
Guoqs
2024-09-02 19:55:02 +08:00
parent a41264d1b9
commit 256e358be5
16 changed files with 245 additions and 115 deletions

View File

@@ -272,6 +272,55 @@ public class BytesUtil {
return stringBuilder.toString();
}
/**
* 将字符串转换为指定长度的16进制字节数组。
*
* @param str 需要转换的字符串
* @param targetLength 目标字节数组的长度
* @return 对应的16进制字节数组
*/
public static byte[] stringToHexBytes(String str, int targetLength) {
if (str == null) {
throw new IllegalArgumentException("字符串不能为null");
}
int strLength = str.length();
if (targetLength < strLength) {
throw new IllegalArgumentException("目标字节数组长度必须大于等于字符串长度");
}
byte[] hexBytes = new byte[targetLength];
for (int i = 0; i < strLength; i++) {
char c = str.charAt(i);
hexBytes[i] = (byte) c;
}
// 剩余部分用0填充
for (int i = strLength; i < targetLength; i++) {
hexBytes[i] = 0;
}
return hexBytes;
}
/**
* 将字节数组转换为十六进制字符串,用于打印查看。
*
* @param bytes 字节数组
* @return 十六进制字符串
*/
private static String bytesToHex(byte[] bytes) {
StringBuilder hexString = new StringBuilder();
for (byte b : bytes) {
String hex = Integer.toHexString(0xFF & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
/**
* @函数功能: 10进制串转为BCD码
* @输入参数: 10进制串
@@ -326,12 +375,14 @@ public class BytesUtil {
return revert(temp);
}
public static void main(String[] args) {
byte[] length = new byte[] {0x09, 0x00};
int i = BytesUtil.bytesToIntLittle(length);
System.out.println(i);
// byte[] length = new byte[] {0x09, 0x00};
// int i = BytesUtil.bytesToIntLittle(length);
// System.out.println(i);
String testStr = "DNY";
byte[] hexBytes = stringToHexBytes(testStr, 3);
System.out.println("Hex Bytes: " + bytesToHex(hexBytes));
}
/**