프로그래밍/flutter

flutter dart 에서 hexString 을 byte 로 변환 (소켓한번만연결)

소행성왕자 2023. 6. 28. 14:56

목적 : flutter 에서 hexString 을 byte 로 변환하여 접속된 소켓으로 보내려고 한다.

javascript 에서는 hexString 을 Uint8Array 로 변환한다. (arrayBuffer)

# inutString
0000012931334331202020202020202020202020646f6d202020202057313931304130312020202020202020202020202020202020202020202020202020202020203030302e3030302e3030302e303030203030302e3030302e3030302e3030302030302d30302d30302d30302d30302d3030202020533031383238352020202020202020205748202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020207b7b2268223a2262617365222c2263223a225731393130413031222c226d223a22726563697665227d7d2020202020202020202020202020202020202020202020202020202020202020202020202020

hex package install

 $ flutter pub add hex

pubspec.yaml   hex: ^0.2.0 추가됨

dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
hex: ^0.2.0

 

:: chatGPT ::

 

현재 mymq 연결하여 응답까지 받아오는것 까지 했음

전체소스 

import 'dart:io';
import 'dart:typed_data';
import 'package:hex/hex.dart';
void main() async {
// const String host = "220.72.212."; // php server
// const int port = 25003;
const String host = "13.125.57."; // mymq
const int port = 9001;
// connect to the socket server
final socket = await Socket.connect(host, port);
print('Connected to: ${socket.remoteAddress.address}:${socket.remotePort}');
// listen for responses from the server
socket.listen(
// handle data from the server
(Uint8List data) {
//final serverResponse = String.fromCharCodes(data);
//print('Server: $serverResponse');
String hexString = HEX.encode(data);
print(hexString);
},
// handle errors
onError: (error) {
print(error);
socket.destroy();
},
// handle server ending connection
onDone: () {
print('Server left.');
socket.destroy();
},
);
// send bytes
const String hexString = "0000012931334331202020202020202020202020646f6d202020202057313931304130312020202020202020202020202020202020202020202020202020202020203030302e3030302e3030302e303030203030302e3030302e3030302e3030302030302d30302d30302d30302d30302d3030202020533031383238352020202020202020205748202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020207b7b2268223a2262617365222c2263223a225731393130413031222c226d223a22726563697665227d7d2020202020202020202020202020202020202020202020202020202020202020202020202020";
List<int> bytes = HEX.decode(hexString);
print(bytes);
socket.add(bytes);
// send String
// await sendMessage(socket, "hellow world");
}
/*
Future<void> sendMessage(Socket socket, String message) async {
print('Client: $message');
*/
/**
* write : 문자열
* add : 바이트
*//*
// socket.write(message);
socket.add(message as List<int>);
await Future.delayed(Duration(seconds: 2));
}*/

:: 응답 ::

 

앞으로 개발해야 될 사항 

 

https://pub.dev/packages/hex/install

 

 

hex | Dart Package

Easy hexadecimal convertion using dart:convert API.

pub.dev