Flutter字节处理插件byte_util的使用
Flutter字节处理插件byte_util的使用
byte_util
是一个用于处理字节数据的 Flutter 插件。它提供了多种功能来操作字节数组,包括读取字符串为字节数组、将字节数组转换为可读字符串、读取 Base64 字符串为字节数组、将字节数组转换为 Base64 字符串、克隆字节数组、深度比较两个字节数组以及提取指定长度的字节数组。
示例代码
以下是一个完整的示例代码,展示了如何使用 byte_util
插件的各种功能。
import 'dart:typed_data';
import 'package:byte_util/byte.dart';
import 'package:byte_util/byte_array.dart';
import 'package:byte_util/byte_double_word.dart';
import 'package:byte_util/byte_util.dart';
import 'package:byte_util/byte_word.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'byte util example',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
[@override](/user/override)
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('byte util example')),
body: Center(child: WidgetExample()),
);
}
}
class WidgetExample extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ElevatedButton(onPressed: testFromReadable, child: Text('从可读字符串转换为字节数组')),
ElevatedButton(onPressed: testToReadable, child: Text('将字节数组转换为可读字符串')),
ElevatedButton(onPressed: testToBase64, child: Text('将字节数组转换为 Base64 字符串')),
ElevatedButton(onPressed: testFromBase64, child: Text('从 Base64 字符串转换为字节数组')),
ElevatedButton(onPressed: testClone, child: Text('克隆字节数组')),
ElevatedButton(onPressed: testSame, child: Text('比较两个字节数组')),
ElevatedButton(onPressed: testExtract, child: Text('提取字节数组')),
],
),
),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ElevatedButton(onPressed: testByteArrayContructor, child: Text('ByteArray 构造器')),
ElevatedButton(onPressed: testByteClass, child: Text('Byte 类')),
],
),
),
],
);
}
void testFromReadable() {
const str1 = '01 02, ff 0x10,0xfa , 90 76 AF a0';
final bytes1 = ByteUtil.fromReadable(str1);
// [1, 2, 255, 16, 250, 144, 118, 175, 160]
print(bytes1);
const str2 = '101 02 90 01,33 90 76 102, 901';
final bytes2 = ByteUtil.fromReadable(str2, radix: Radix.dec);
// [101, 2, 90, 1, 33, 90, 76, 102, 133]
print(bytes2);
}
void testToReadable() {
final bytes = Uint8List.fromList([0x80, 01, 02, 0xff, 0xA1, 30, 10, 20, 77]);
final str1 = ByteUtil.toReadable(bytes);
// 0x80 0x1 0x2 0xFF 0xA1 0x1E 0xA 0x14 0x4D
print(str1);
final str2 = ByteUtil.toReadable(bytes, radix: Radix.dec);
// 128 1 2 255 161 30 10 20 77
print(str2);
}
void testToBase64() {
final bytes = Uint8List.fromList([0x80, 01, 02, 0xff, 0xA1, 30, 10, 32]);
final base64 = ByteUtil.toBase64(bytes);
// gAEC/6EeCiA=
print(base64);
}
void testFromBase64() {
final base64 = 'gAEC/6EeCiA=';
final bytes = ByteUtil.fromBase64(base64);
// [128, 1, 2, 255, 161, 30, 10, 32]
print(bytes);
}
void testClone() {
final bytes = Uint8List.fromList([0x80, 01, 02, 0xff, 0xA1, 30, 10, 32]);
final clone = ByteUtil.clone(bytes);
// [128, 1, 2, 255, 161, 30, 10, 32]
print(clone);
}
void testSame() {
final bytes1 = Uint8List.fromList([0x80, 01, 02, 0xff, 0xA1, 30, 10, 32]);
final bytes2 = Uint8List.fromList([0xA1, 30, 10, 32]);
final same = ByteUtil.same(bytes1, bytes2);
// false
print(same);
}
void testExtract() {
final bytes = Uint8List.fromList([0x80, 01, 02, 0xff, 0xA1, 30, 10, 32]);
// 0x1 0x2 0xFF
print(ByteUtil.toReadable(
ByteUtil.extract(origin: bytes, indexStart: 1, length: 3)));
// null
print(ByteUtil.toReadable(
ByteUtil.extract(origin: bytes, indexStart: 0, length: 0)));
// 0x80 0x1 0x2 0xFF 0xA1 0x1E 0xA 0x14 0x20
print(ByteUtil.toReadable(
ByteUtil.extract(origin: bytes, indexStart: 0, length: 100)));
// null
print(ByteUtil.toReadable(
ByteUtil.extract(origin: bytes, indexStart: 10, length: 8)));
// 0x80 0x1 0x2 0xFF 0xA1 0x1E 0xA 0x20
print(ByteUtil.toReadable(
ByteUtil.extract(origin: bytes, indexStart: 0, length: 8)));
// null
print(ByteUtil.toReadable(
ByteUtil.extract(origin: bytes, indexStart: 8, length: 1)));
// 0x20
print(ByteUtil.toReadable(
ByteUtil.extract(origin: bytes, indexStart: 7, length: 1)));
}
void testByteArrayContructor() {
// [1, 2, 3]
final arr1 = ByteArray(Uint8List.fromList([1, 2, 3]));
print(arr1.bytes);
// [3]
final arr2 = ByteArray.fromByte(3);
print(arr2.bytes);
// [1, 2, 3, 4, 5, 6]
final arr3 = ByteArray.combineArrays(
Uint8List.fromList([1, 2, 3]), Uint8List.fromList([4, 5, 6]));
print(arr3.bytes);
// [1, 2, 3, 7]
final arr4 = ByteArray.combine1(Uint8List.fromList([1, 2, 3]), 7);
print(arr4.bytes);
// [8, 1, 2, 3]
final arr5 = ByteArray.combine2(8, Uint8List.fromList([1, 2, 3]));
print(arr5.bytes);
// [8, 1, 2, 3, 10]
arr5.append(10);
print(arr5.bytes);
// [8, 1, 2, 3, 10, 9, 9]
arr5.appendArray(Uint8List.fromList([9, 9]));
print(arr5.bytes);
// [8, 12, 1, 2, 3, 10, 9, 9]
arr5.insert(indexStart: 1, value: 12);
print(arr5.bytes);
// [8, 12, 1, 2, 3, 10, 9, 9, 13]
arr5.insert(indexStart: 100, value: 13);
print(arr5.bytes);
// [8, 12, 1, 23, 23, 2, 3, 10, 9, 9, 13]
arr5.insertArray(indexStart: 3, arrayInsert: Uint8List.fromList([23, 23]));
print(arr5.bytes);
// [12, 1, 23, 23, 2, 3, 10, 9, 9, 13]
arr5.remove(indexStart: 0, lengthRemove: 1);
print(arr5.bytes);
// [12, 1, 23]
arr5.remove(indexStart: 3, lengthRemove: 9);
print(arr5.bytes);
}
void testByteClass() {
final byte1 = Byte(3);
final byte2 = Byte(0xA1);
final byte3 = Byte(12);
final byte4 = Byte(65);
// 03
print(byte1);
final word = ByteWord(high: byte2, low: byte1);
// A1,03
print(word);
final doubleWord =
ByteDoubleWord(one: byte1, two: byte2, three: byte3, four: byte4);
// 41,0C,A1,03
print(doubleWord);
final dw = ByteDoubleWord.fromInt(184384451);
// 0x0A,0xFD,0x7B,0xC3
print(dw);
}
}
更多关于Flutter字节处理插件byte_util的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter字节处理插件byte_util的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,我可以为你提供一个关于如何在Flutter项目中使用byte_util
插件的示例。byte_util
是一个用于字节处理的Flutter插件,可以方便地进行字节数组的各种操作。
首先,确保你已经在pubspec.yaml
文件中添加了byte_util
依赖:
dependencies:
flutter:
sdk: flutter
byte_util: ^x.y.z # 请替换为最新的版本号
然后运行flutter pub get
来安装依赖。
下面是一个简单的Flutter应用示例,展示如何使用byte_util
进行字节处理:
import 'package:flutter/material.dart';
import 'package:byte_util/byte_util.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Byte Util Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: ByteUtilDemo(),
);
}
}
class ByteUtilDemo extends StatefulWidget {
@override
_ByteUtilDemoState createState() => _ByteUtilDemoState();
}
class _ByteUtilDemoState extends State<ByteUtilDemo> {
String result = '';
void _convertStringToBytes() {
String input = "Hello, Flutter!";
List<int> bytes = ByteUtil.stringToBytes(input, encoding: Encoding.UTF8);
setState(() {
result = 'String to Bytes: ${bytes.join(', ')}';
});
}
void _convertBytesToString() {
List<int> bytes = [72, 101, 108, 108, 111, 44, 32, 70, 108, 117, 116, 116, 101, 114, 33];
String output = ByteUtil.bytesToString(bytes, encoding: Encoding.UTF8);
setState(() {
result = 'Bytes to String: $output';
});
}
void _convertHexToBytes() {
String hex = "48656c6c6f2c20466c757474657221";
List<int> bytes = ByteUtil.hexToBytes(hex);
setState(() {
result = 'Hex to Bytes: ${bytes.join(', ')}';
});
}
void _convertBytesToHex() {
List<int> bytes = [72, 101, 108, 108, 111, 44, 32, 70, 108, 117, 116, 116, 101, 114, 33];
String hex = ByteUtil.bytesToHex(bytes);
setState(() {
result = 'Bytes to Hex: $hex';
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Byte Util Demo'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Result:', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
SizedBox(height: 8),
Text(result, style: TextStyle(fontSize: 16)),
SizedBox(height: 24),
ElevatedButton(
onPressed: _convertStringToBytes,
child: Text('String to Bytes'),
),
SizedBox(height: 8),
ElevatedButton(
onPressed: _convertBytesToString,
child: Text('Bytes to String'),
),
SizedBox(height: 8),
ElevatedButton(
onPressed: _convertHexToBytes,
child: Text('Hex to Bytes'),
),
SizedBox(height: 8),
ElevatedButton(
onPressed: _convertBytesToHex,
child: Text('Bytes to Hex'),
),
],
),
),
);
}
}
在这个示例中,我们展示了如何使用byte_util
进行以下操作:
- 将字符串转换为字节数组。
- 将字节数组转换回字符串。
- 将十六进制字符串转换为字节数组。
- 将字节数组转换为十六进制字符串。
你可以根据需求进一步扩展这个示例,或者将其集成到你的Flutter项目中。记得根据byte_util
插件的实际API文档调整代码,因为插件的API可能会随版本更新而发生变化。