1
0

设备mqtt收发数据

This commit is contained in:
2025-07-30 08:50:44 +08:00
parent 2b2edf096d
commit ac353b1078
21 changed files with 679 additions and 105 deletions

View File

@ -310,4 +310,64 @@ public class ImageToCArrayConverter {
return new byte[0];
}
}
/**
* 将字节字符串转换为混合类型的Object数组
*
* @param byteString 字节字符串
* @return Object数组包含不同类型的对象
*/
public static Object[] convertByteStringToMixedObjectArray(String byteString) {
if (byteString == null || byteString.isEmpty()) {
return new Object[0];
}
try {
// 移除方括号(如果存在)
String content = byteString.trim();
if (content.startsWith("[")) {
content = content.substring(1);
}
if (content.endsWith("]")) {
content = content.substring(0, content.length() - 1);
}
// 按逗号分割
String[] byteValues = content.split(",");
Object[] result = new Object[byteValues.length];
// 转换每个值为适当类型的对象
for (int i = 0; i < byteValues.length; i++) {
String value = byteValues[i].trim();
// 处理可能的进制前缀
if (value.startsWith("0x") || value.startsWith("0X")) {
// 十六进制
int intValue = Integer.parseInt(value.substring(2), 16);
result[i] = intValue;
} else {
// 尝试解析为整数
try {
int intValue = Integer.parseInt(value);
// 根据值的范围选择合适的类型
if (intValue >= Byte.MIN_VALUE && intValue <= Byte.MAX_VALUE) {
result[i] = (byte) intValue;
} else if (intValue >= Short.MIN_VALUE && intValue <= Short.MAX_VALUE) {
result[i] = (short) intValue;
} else {
result[i] = intValue;
}
} catch (NumberFormatException e) {
// 如果不是数字,保持为字符串
result[i] = value;
}
}
}
return result;
} catch (Exception e) {
System.err.println("解析字节字符串时出错: " + e.getMessage());
return new Object[0];
}
}
}