329 lines
8.9 KiB
Dart
Executable File
329 lines
8.9 KiB
Dart
Executable File
|
||
import 'dart:convert';
|
||
import 'dart:typed_data';
|
||
|
||
import 'package:crypto/crypto.dart';
|
||
import 'package:flutter/services.dart';
|
||
|
||
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;
|
||
}
|
||
|
||
List<String> changeIntListToStringList(List<int> list){
|
||
final List<String> strList = <String>[];
|
||
for(int i = 0; i<list.length; i++)
|
||
{
|
||
strList.add(list[i].toRadixString(16));
|
||
}
|
||
return strList;
|
||
}
|
||
|
||
List<int> changeStringListToIntList(List<String> list){
|
||
final List<int> intList = <int>[];
|
||
for(int i = 0; i<list.length; i++)
|
||
{
|
||
intList.add(_hexToInt(list[i]));
|
||
}
|
||
return intList;
|
||
}
|
||
|
||
int _hexToInt(String hex) {
|
||
int val = 0;
|
||
final int len = hex.length;
|
||
for (int i = 0; i < len; i++) {
|
||
final int hexDigit = hex.codeUnitAt(i);
|
||
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 {
|
||
throw const FormatException('Invalid hexadecimal value');
|
||
}
|
||
}
|
||
return val;
|
||
}
|
||
|
||
String md5Crypto(List<int> data) {
|
||
final Digest dig = md5.convert(data);
|
||
final String keyStr = dig.toString();
|
||
return keyStr.substring(0, 16).toLowerCase();
|
||
}
|
||
|
||
// 获取固定长度的数组
|
||
List<int> getFixedLengthList(List<int> data, int length) {
|
||
for (int i = 0; i < length; i++) {
|
||
data.add(0);
|
||
}
|
||
return data;
|
||
}
|
||
|
||
//int ---> 指定长度的hex (如指定长度为6的情况,0x000001 0x001234, 0xefab23)
|
||
String intToFormatHex(int num, int length) {
|
||
final String hexString = num.toRadixString(16);
|
||
final String formatString = hexString.padLeft(length, '0');
|
||
return formatString;
|
||
}
|
||
|
||
String uint8ToHex(Uint8List byteArr) {
|
||
if (byteArr.isEmpty) {
|
||
return '';
|
||
}
|
||
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
|
||
result[i2] = hexTable[index].codeUnitAt(0); //左边的值取字符表,转为Unicode放进resut数组
|
||
index = bit & 15; //取右边四位
|
||
result[i2 + 1] =
|
||
hexTable[index].codeUnitAt(0); //右边的值取字符表,转为Unicode放进resut数组
|
||
}
|
||
return String.fromCharCodes(result); //Unicode转回为对应字符,生成字符串返回
|
||
}
|
||
|
||
// int->两个字节 List 低字节在前,高字节在后(大端存储)=>嵌入式小端接收(协议用到)
|
||
|
||
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);
|
||
}
|
||
|
||
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);
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
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 |
|
||
(0xFF & dataDetail[index + 2]) << 16 |
|
||
(0xFF & dataDetail[index + 1]) << 8 |
|
||
0xFF & dataDetail[index];
|
||
return _toInt32(value);
|
||
}
|
||
|
||
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
|
||
List<int> intToByte4ListHigh(int value) =>
|
||
<int>[value, value >> 8, value >> 16, value >> 24];
|
||
|
||
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);
|
||
}
|
||
|
||
int byteUInt16(List<int> dataDetail, int index) =>
|
||
_byteUInt16(dataDetail, index);
|
||
int _byteUInt16(List<int> dataDetail, int index) {
|
||
if (checkListIndex(dataDetail, index, 2)) {
|
||
return 0;
|
||
}
|
||
final int value = (dataDetail[index + 1]) << 8 | dataDetail[index];
|
||
return _toUInt16(value);
|
||
}
|
||
|
||
int _toUInt16(int value) {
|
||
final ByteBuffer buffer = Uint16List(2).buffer;
|
||
final ByteData bData = ByteData.view(buffer);
|
||
bData.setInt16(0, value);
|
||
return bData.getUint16(0);
|
||
}
|
||
|
||
int byteUInt32(List<int> dataDetail, int index) =>
|
||
_byteUInt32(dataDetail, index);
|
||
|
||
int _byteUInt32(List<int> dataDetail, int index) {
|
||
if (checkListIndex(dataDetail, index, 4)) {
|
||
return 0;
|
||
}
|
||
final int value = (0xFF & dataDetail[(index + 3)]) << 24 |
|
||
(0xFF & dataDetail[index + 2]) << 16 |
|
||
(0xFF & dataDetail[index + 1]) << 8 |
|
||
0xFF & dataDetail[index];
|
||
return _toUInt32(value);
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
bool checkListIndex(List<int> dataDetail, int index, int offsetLength) {
|
||
final int len = dataDetail.length;
|
||
final bool result = index + offsetLength > len;
|
||
if (result) {}
|
||
return result;
|
||
}
|
||
|
||
int checkSum(List<int> data) {
|
||
int sum = 0;
|
||
for (final int v in data) {
|
||
sum ^= v;
|
||
}
|
||
return sum;
|
||
}
|
||
|
||
Future<List<int>> loadAssetsFile(String assetsPath) async {
|
||
final ByteData byteData = await rootBundle.load(assetsPath);
|
||
return byteData.buffer.asUint8List().toList();
|
||
}
|
||
|
||
//获取截取后的数组
|
||
List<List<T>> splitList<T>(List<T> list, int len) {
|
||
if (len <= 1) {
|
||
return <List<T>>[list];
|
||
}
|
||
|
||
final List<List<T>> result = <List<T>>[];
|
||
int index = 1;
|
||
|
||
while (true) {
|
||
if (index * len < list.length) {
|
||
final List<T> temp = list.skip((index - 1) * len).take(len).toList();
|
||
result.add(temp);
|
||
index++;
|
||
continue;
|
||
}
|
||
final List<T> temp = list.skip((index - 1) * len).toList();
|
||
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) {
|
||
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;
|
||
}
|
||
|
||
String asciiString(List<int> codeUnits) {
|
||
String result = '';
|
||
for (final int value in codeUnits) {
|
||
result += String.fromCharCode(value);
|
||
}
|
||
return result;
|
||
}
|
||
|
||
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 '';
|
||
}
|
||
}
|
||
// 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();
|
||
// }
|
||
|
||
bool compareTwoList({List<int>? list1, List<int>? list2}) {
|
||
if (list1!.length != list2!.length) {
|
||
return false;
|
||
}
|
||
final int ctn = list1.length;
|
||
for (int i = 0; i < ctn; i++) {
|
||
final int v1 = list1[i];
|
||
final int v2 = list2[i];
|
||
if (v1 != v2) {
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
List<int> encodeStringToInt(String input) => utf8.encode(input);
|