简体   繁体   中英

Convert Datagram Data To String HEX

I'm Listening To UDP 6000 port and I want to convert incoming packages to Hex String

I searched but nothing found

Here is my code

import 'dart:io';

import 'package:udp/udp.dart';

 main() async {
  var sender = await UDP.bind(Endpoint.loopback(port: Port(42)));

 var dataLength = await sender.send(
  "Hello World!".codeUnits, Endpoint.broadcast(port: Port(21)));

  stdout.write("${dataLength} bytes sent.");

  var receiver =
   await UDP.bind(Endpoint.unicast(InternetAddress.anyIPv4, Port(6000)));

    await receiver.listen((datagram) {
    print(datagram.data);
   }, Duration(seconds: 200));

  sender.close();
  receiver.close();
}
#

my incoming package is like this:

[104, 101, 108, 108, 111]

I want to Convert to this

68656c6c6f

thanks

You can use the toRadixString to convert to values to hex, and the map the list.

var hex = datagram.data.map((e) => e.toRadixString(16));
print(hex);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM