33 lines
859 B
Dart
33 lines
859 B
Dart
abstract class SerialTool {
|
|
/// 转16进制转字符串
|
|
static String bytesToString(List<int> intList) {
|
|
return intList
|
|
.map((intVal) => intVal.toRadixString(16).toUpperCase().padLeft(2, '0'))
|
|
.join();
|
|
}
|
|
|
|
/// 转16进制转字符串
|
|
static List<int> stringToBytes(String strCode) {
|
|
List<int> bytes = [];
|
|
for (int i = 0; i < strCode.length; i += 2) {
|
|
String hexPair = strCode.substring(i, i + 2);
|
|
int byteValue = int.parse(hexPair, radix: 16);
|
|
bytes.add(byteValue);
|
|
}
|
|
return bytes;
|
|
}
|
|
|
|
/// 字符串转16进制不够补零
|
|
static String? toHexPadded({
|
|
required String str,
|
|
int max = 32,
|
|
}) {
|
|
String hex =
|
|
str.codeUnits.map((c) => c.toRadixString(16).padLeft(2, '0')).join();
|
|
if (hex.length > max) {
|
|
return null;
|
|
}
|
|
return hex.padRight(max, '0');
|
|
}
|
|
}
|