329 lines
8.9 KiB
Dart
Raw Permalink Normal View History

import 'dart:convert';
import 'dart:typed_data';
2023-08-10 09:52:05 +08:00
import 'package:crypto/crypto.dart';
import 'package:flutter/services.dart';
2023-08-10 18:56:29 +08:00
int listChangInt(List<int> list){
final int dataLen = (list[0] << 8) + list[1];
return dataLen;
}
List<int> intChangList(int count){
final List<int> intList = <int>[];
final double typeDouble = count / 256;
final int type1 = typeDouble.toInt();
final int type2 = count % 256;
intList.add(type1);
intList.add(type2);
return intList;
}
2023-08-12 18:32:49 +08:00
List<String> changeIntListToStringList(List<int> list){
final List<String> strList = <String>[];
2023-08-12 18:32:49 +08:00
for(int i = 0; i<list.length; i++)
{
strList.add(list[i].toRadixString(16));
}
return strList;
}
2023-08-10 18:56:29 +08:00
2023-08-12 18:32:49 +08:00
List<int> changeStringListToIntList(List<String> list){
final List<int> intList = <int>[];
2023-08-12 18:32:49 +08:00
for(int i = 0; i<list.length; i++)
{
intList.add(_hexToInt(list[i]));
}
return intList;
}
2023-08-10 18:56:29 +08:00
2023-08-12 18:32:49 +08:00
int _hexToInt(String hex) {
int val = 0;
final int len = hex.length;
2023-08-12 18:32:49 +08:00
for (int i = 0; i < len; i++) {
final int hexDigit = hex.codeUnitAt(i);
2023-08-12 18:32:49 +08:00
if (hexDigit >= 48 && hexDigit <= 57) {
val += (hexDigit - 48) * (1 << (4 * (len - 1 - i)));
} else if (hexDigit >= 65 && hexDigit <= 70) {
// A..F
val += (hexDigit - 55) * (1 << (4 * (len - 1 - i)));
} else if (hexDigit >= 97 && hexDigit <= 102) {
// a..f
val += (hexDigit - 87) * (1 << (4 * (len - 1 - i)));
} else {
2024-08-20 09:53:39 +08:00
throw const FormatException('Invalid hexadecimal value');
2023-08-12 18:32:49 +08:00
}
}
return val;
}
2023-08-09 09:32:09 +08:00
String md5Crypto(List<int> data) {
final Digest dig = md5.convert(data);
final String keyStr = dig.toString();
2023-08-10 09:52:05 +08:00
return keyStr.substring(0, 16).toLowerCase();
}
2023-08-09 15:42:26 +08:00
// 获取固定长度的数组
List<int> getFixedLengthList(List<int> data, int length) {
for (int i = 0; i < length; i++) {
2023-08-09 15:42:26 +08:00
data.add(0);
}
return data;
}
2023-08-08 09:42:35 +08:00
//int ---> 指定长度的hex (如指定长度为6的情况,0x000001 0x001234, 0xefab23)
2023-08-09 09:32:09 +08:00
String intToFormatHex(int num, int length) {
final String hexString = num.toRadixString(16);
2024-08-20 09:53:39 +08:00
final String formatString = hexString.padLeft(length, '0');
2023-08-08 09:42:35 +08:00
return formatString;
}
String uint8ToHex(Uint8List byteArr) {
if (byteArr.isEmpty) {
2024-08-20 09:53:39 +08:00
return '';
2023-08-08 09:42:35 +08:00
}
final Uint8List result = Uint8List(byteArr.length << 1);
final List<String> hexTable = <String>[
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F'
]; //16进制字符表
for (int i = 0; i < byteArr.length; i++) {
final int bit = byteArr[i]; //取传入的byteArr的每一位
int index = bit >> 4 & 15; //右移4位,取剩下四位
final int i2 = i << 1; //byteArr的每一位对应结果的两位,所以对于结果的操作位数要乘2
2023-08-08 09:42:35 +08:00
result[i2] = hexTable[index].codeUnitAt(0); //左边的值取字符表,转为Unicode放进resut数组
index = bit & 15; //取右边四位
2023-08-09 09:35:10 +08:00
result[i2 + 1] =
hexTable[index].codeUnitAt(0); //右边的值取字符表,转为Unicode放进resut数组
2023-08-08 09:42:35 +08:00
}
return String.fromCharCodes(result); //Unicode转回为对应字符,生成字符串返回
}
// int->两个字节 List 低字节在前,高字节在后(大端存储)=>嵌入式小端接收(协议用到)
2023-08-09 09:35:10 +08:00
int byteInt8(List<int> dataDetail, int index) => _toInt8(dataDetail[index]);
int _toInt8(int value) {
final ByteBuffer buffer = Int8List(4).buffer;
final ByteData bData = ByteData.view(buffer);
bData.setInt8(0, value);
return bData.getInt8(0);
}
2023-08-09 09:35:10 +08:00
int byteInt16(List<int> dataDetail, int index) => _byteInt16(dataDetail, index);
int _byteInt16(List<int> dataDetail, int index) {
if (checkListIndex(dataDetail, index, 2)) {
return 0;
}
final int value = (dataDetail[index + 1] & 0xff) << 8 | (dataDetail[index] & 0xff);
return _toInt16(value);
}
2023-08-09 09:35:10 +08:00
int _toInt16(int value) {
final ByteBuffer buffer = Int16List(1).buffer;
final ByteData bData = ByteData.view(buffer);
bData.setInt16(0, value);
final int outputValue = bData.getInt16(0);
return outputValue;
}
2023-08-09 09:35:10 +08:00
int byteInt32(List<int> dataDetail, int index) => _byteInt32(dataDetail, index);
int _byteInt32(List<int> dataDetail, int index) {
if (checkListIndex(dataDetail, index, 4)) {
return 0;
}
final int value = (0xFF & dataDetail[(index + 3)]) << 24 |
2023-08-09 09:35:10 +08:00
(0xFF & dataDetail[index + 2]) << 16 |
(0xFF & dataDetail[index + 1]) << 8 |
0xFF & dataDetail[index];
return _toInt32(value);
}
2023-08-09 09:35:10 +08:00
int _toInt32(int value) {
final ByteBuffer buffer = Int32List(2).buffer;
final ByteData bData = ByteData.view(buffer);
bData.setInt32(0, value);
final int outputValue = bData.getInt32(0);
return outputValue;
}
List<int> intToInt8List(int value) => <int>[value];
List<int> intToByte2ListHigh(int value) => <int>[value, value >> 8];
// int->4个字节List 低字节在前,高字节在后(大端存储) 1byte = 8bit
2023-08-09 09:35:10 +08:00
List<int> intToByte4ListHigh(int value) =>
<int>[value, value >> 8, value >> 16, value >> 24];
2023-08-09 09:35:10 +08:00
int byteUInt8(List<int> dataDetail, int index) => _toUInt8(dataDetail[index]);
int _toUInt8(int value) {
final ByteBuffer buffer = Int8List(1).buffer;
final ByteData bData = ByteData.view(buffer);
bData.setInt8(0, value);
return bData.getUint8(0);
}
2023-08-09 09:35:10 +08:00
int byteUInt16(List<int> dataDetail, int index) =>
_byteUInt16(dataDetail, index);
int _byteUInt16(List<int> dataDetail, int index) {
2023-08-09 09:35:10 +08:00
if (checkListIndex(dataDetail, index, 2)) {
return 0;
}
final int value = (dataDetail[index + 1]) << 8 | dataDetail[index];
return _toUInt16(value);
}
2023-08-09 09:35:10 +08:00
int _toUInt16(int value) {
final ByteBuffer buffer = Uint16List(2).buffer;
final ByteData bData = ByteData.view(buffer);
bData.setInt16(0, value);
return bData.getUint16(0);
}
2023-08-09 09:35:10 +08:00
int byteUInt32(List<int> dataDetail, int index) =>
_byteUInt32(dataDetail, index);
2023-08-09 09:35:10 +08:00
int _byteUInt32(List<int> dataDetail, int index) {
if (checkListIndex(dataDetail, index, 4)) {
return 0;
}
final int value = (0xFF & dataDetail[(index + 3)]) << 24 |
2023-08-09 09:35:10 +08:00
(0xFF & dataDetail[index + 2]) << 16 |
(0xFF & dataDetail[index + 1]) << 8 |
0xFF & dataDetail[index];
return _toUInt32(value);
}
2023-08-09 09:35:10 +08:00
int _toUInt32(int value) {
final ByteBuffer buffer = Int32List(2).buffer;
final ByteData bData = ByteData.view(buffer);
bData.setInt32(0, value);
final int outputValue = bData.getUint32(0);
return outputValue;
}
2023-08-09 09:35:10 +08:00
bool checkListIndex(List<int> dataDetail, int index, int offsetLength) {
final int len = dataDetail.length;
final bool result = index + offsetLength > len;
2023-08-09 09:35:10 +08:00
if (result) {}
return result;
}
2023-08-09 09:35:10 +08:00
int checkSum(List<int> data) {
int sum = 0;
for (final int v in data) {
sum ^= v;
}
return sum;
}
2023-08-09 09:35:10 +08:00
Future<List<int>> loadAssetsFile(String assetsPath) async {
final ByteData byteData = await rootBundle.load(assetsPath);
return byteData.buffer.asUint8List().toList();
}
2023-08-09 09:35:10 +08:00
//获取截取后的数组
List<List<T>> splitList<T>(List<T> list, int len) {
if (len <= 1) {
return <List<T>>[list];
2023-08-09 09:35:10 +08:00
}
final List<List<T>> result = <List<T>>[];
2023-08-09 09:35:10 +08:00
int index = 1;
while (true) {
if (index * len < list.length) {
final List<T> temp = list.skip((index - 1) * len).take(len).toList();
2023-08-09 09:35:10 +08:00
result.add(temp);
index++;
continue;
}
final List<T> temp = list.skip((index - 1) * len).toList();
2023-08-09 09:35:10 +08:00
result.add(temp);
break;
}
return result;
}
// int->两个字节 List 高字节在前,低字节在后(小端存储) 本工程只有配置 WiFi用到的!!!!
List<int> intToByte2ListLow(int value) => <int>[value >> 8, value];
String radixHex16String(List<int> codeUnits) {
String result = '';
for (final int value in codeUnits) {
2023-08-09 09:35:10 +08:00
result += value.toRadixString(16).padLeft(2, '0');
}
return result;
}
String radixHex16StringTo2String(List<int> codeUnits) {
String result = '';
codeUnits.forEach((int value) {
result += int.parse(value.toRadixString(16).padLeft(2, '0'),radix: 16).toRadixString(2);
});
return result;
}
2023-08-09 09:35:10 +08:00
String asciiString(List<int> codeUnits) {
String result = '';
for (final int value in codeUnits) {
result += String.fromCharCode(value);
}
return result;
}
2023-08-09 09:35:10 +08:00
String utf8String(List<int> codeUnits) {
try {
// 只去除结尾的0常见填充方式
int realLen = codeUnits.indexWhere((b) => b == 0);
List<int> validBytes = (realLen == -1) ? codeUnits : codeUnits.sublist(0, realLen);
return utf8.decode(validBytes);
} catch (e) {
// 容错处理
return '';
2023-11-01 17:28:59 +08:00
}
}
// String utf8String(List<int> codeUnits) {
// codeUnits.reversed;
// final List<int> uniqueList = <int>[];
// for (int i = 0; i < codeUnits.length; i++) {
// if (codeUnits[i] != 0) {
// uniqueList.add(codeUnits[i]);
// }
// }
// uniqueList.reversed;
// return utf8.decode(uniqueList).toString();
// }
2023-08-09 09:35:10 +08:00
bool compareTwoList({List<int>? list1, List<int>? list2}) {
if (list1!.length != list2!.length) {
return false;
}
final int ctn = list1.length;
2023-08-09 09:35:10 +08:00
for (int i = 0; i < ctn; i++) {
final int v1 = list1[i];
final int v2 = list2[i];
2023-08-09 09:35:10 +08:00
if (v1 != v2) {
return false;
}
}
return true;
}
2023-08-09 09:35:10 +08:00
List<int> encodeStringToInt(String input) => utf8.encode(input);