我正在try 将int转换为byte,但找不到适合我的示例

我需要将字节中的int转换为tcp ip中发送的字节

final server = await ServerSocket.bind(ip,port);
try{
  server.listen((Socket socket) {
    int a = 10; // line feed
    int b = 36; // $
    int c = 122; // z
    const sec = Duration(seconds:1);
    Timer.periodic(sec,(Timer t) {
      socket.wite('data : $a,$b,$c');
    });
} on SocketExeption catch(ex) {
    print(ex.message);
}

我收到了

|d|a|t|a| |:|1|0|,|3|6|,|1|2|2|

我如何接受

|d|a|t|a| |:|LF|,|$|,|z| or |d|a|t|a| |:|0x0A|,|$|,|z|

我和plc一起工作,他喜欢绝对索引中的字节

有可能吗?

如果使用

int a = 0;
for(;;){
  socket.write(String.fromCharCode(a));
  a++;
  if(a>=256)a=0;
}

通过tcp ip接收

byte0 =00.01....4F........7D.7E.7F.C2.C2....C2.C2..C2.C2.C3.C3.C3..C3.C3.00.01...
byte1 =00.00....00........00.00.00.80.81....84.85..BE.BF.80.81.82..BE.BF.00.00...

In any case i can use strings but for some protocols it's a huge of data, minimun moltiply x4 如果使用 3 char 和 one separator

我try 在windows应用程序上使用RTC和Flutter

对于上一个项目,我创建了

javaServer<-low side code bit/byte->plc 

flutter<-json->javaServer

我觉得很难看

工作感谢麻风

  try {
    client.listen((Socket socket) {
      debugPrint('New TCP client ${socket.address.address}:${socket.port} connected.');
      List<int> bytes = List.empty(growable: true);
      for (var i = 0; i < 255; i++) {
        bytes.add(i);
      }
      Timer.periodic(const Duration(seconds: 1), (Timer t) {
        for (var i = 0; i < 255; i++) {
          socket.add([bytes[i]]);
        }
      });
    });
  } on SocketException catch (ex) {
    debugPrint(ex.message);
    Timer.periodic(Duration(seconds: 1), (Timer t) {
      startServer();
    });
  }

推荐答案

应该使用add来写入字节列表,而不是使用socket.write来写入字符.

从所有字节从0到255的示例中,您需要执行以下操作:

socket.write('data : ');
String separator = '';
for (int a = 0; a < 256; a++) {
  if (separator != null) socket.write(separator);
  socket.add([a]);
  separator = ',';
}

通过以上内容,您将在客户端获得如下内容:

00000000: 6461 7461 203a 2000 2c01 2c02 2c03 2c04  data : .,.,.,.,.
00000010: 2c05 2c06 2c07 2c08 2c09 2c0a 2c0b 2c0c  ,.,.,.,.,.,.,.,.
00000020: 2c0d 2c0e 2c0f 2c10 2c11 2c12 2c13 2c14  ,.,.,.,.,.,.,.,.
00000030: 2c15 2c16 2c17 2c18 2c19 2c1a 2c1b 2c1c  ,.,.,.,.,.,.,.,.
00000040: 2c1d 2c1e 2c1f 2c20 2c21 2c22 2c23 2c24  ,.,.,., ,!,",#,$
00000050: 2c25 2c26 2c27 2c28 2c29 2c2a 2c2b 2c2c  ,%,&,',(,),*,+,,
00000060: 2c2d 2c2e 2c2f 2c30 2c31 2c32 2c33 2c34  ,-,.,/,0,1,2,3,4
00000070: 2c35 2c36 2c37 2c38 2c39 2c3a 2c3b 2c3c  ,5,6,7,8,9,:,;,<
00000080: 2c3d 2c3e 2c3f 2c40 2c41 2c42 2c43 2c44  ,=,>,?,@,A,B,C,D
00000090: 2c45 2c46 2c47 2c48 2c49 2c4a 2c4b 2c4c  ,E,F,G,H,I,J,K,L
000000a0: 2c4d 2c4e 2c4f 2c50 2c51 2c52 2c53 2c54  ,M,N,O,P,Q,R,S,T
000000b0: 2c55 2c56 2c57 2c58 2c59 2c5a 2c5b 2c5c  ,U,V,W,X,Y,Z,[,\
000000c0: 2c5d 2c5e 2c5f 2c60 2c61 2c62 2c63 2c64  ,],^,_,`,a,b,c,d
000000d0: 2c65 2c66 2c67 2c68 2c69 2c6a 2c6b 2c6c  ,e,f,g,h,i,j,k,l
000000e0: 2c6d 2c6e 2c6f 2c70 2c71 2c72 2c73 2c74  ,m,n,o,p,q,r,s,t
000000f0: 2c75 2c76 2c77 2c78 2c79 2c7a 2c7b 2c7c  ,u,v,w,x,y,z,{,|
00000100: 2c7d 2c7e 2c7f 2c80 2c81 2c82 2c83 2c84  ,},~,.,.,.,.,.,.
00000110: 2c85 2c86 2c87 2c88 2c89 2c8a 2c8b 2c8c  ,.,.,.,.,.,.,.,.
00000120: 2c8d 2c8e 2c8f 2c90 2c91 2c92 2c93 2c94  ,.,.,.,.,.,.,.,.
00000130: 2c95 2c96 2c97 2c98 2c99 2c9a 2c9b 2c9c  ,.,.,.,.,.,.,.,.
00000140: 2c9d 2c9e 2c9f 2ca0 2ca1 2ca2 2ca3 2ca4  ,.,.,.,.,.,.,.,.
00000150: 2ca5 2ca6 2ca7 2ca8 2ca9 2caa 2cab 2cac  ,.,.,.,.,.,.,.,.
00000160: 2cad 2cae 2caf 2cb0 2cb1 2cb2 2cb3 2cb4  ,.,.,.,.,.,.,.,.
00000170: 2cb5 2cb6 2cb7 2cb8 2cb9 2cba 2cbb 2cbc  ,.,.,.,.,.,.,.,.
00000180: 2cbd 2cbe 2cbf 2cc0 2cc1 2cc2 2cc3 2cc4  ,.,.,.,.,.,.,.,.
00000190: 2cc5 2cc6 2cc7 2cc8 2cc9 2cca 2ccb 2ccc  ,.,.,.,.,.,.,.,.
000001a0: 2ccd 2cce 2ccf 2cd0 2cd1 2cd2 2cd3 2cd4  ,.,.,.,.,.,.,.,.
000001b0: 2cd5 2cd6 2cd7 2cd8 2cd9 2cda 2cdb 2cdc  ,.,.,.,.,.,.,.,.
000001c0: 2cdd 2cde 2cdf 2ce0 2ce1 2ce2 2ce3 2ce4  ,.,.,.,.,.,.,.,.
000001d0: 2ce5 2ce6 2ce7 2ce8 2ce9 2cea 2ceb 2cec  ,.,.,.,.,.,.,.,.
000001e0: 2ced 2cee 2cef 2cf0 2cf1 2cf2 2cf3 2cf4  ,.,.,.,.,.,.,.,.
000001f0: 2cf5 2cf6 2cf7 2cf8 2cf9 2cfa 2cfb 2cfc  ,.,.,.,.,.,.,.,.
00000200: 2cfd 2cfe 2cff                           ,.,.,.

Flutter相关问答推荐

如何在Flutter中的textformfield上显示标签文本?

关闭视频通话,相机仍在运行!(Flutter WebRTC)

如何使用Firebase Firestore来获取外部集合中的文档子集合中的文档

底部导航栏项目在抖动中不更改 colored颜色

MobX Build_Runner问题:不生成.G文件(Flutter )

SupabaseFlutter 集成问题:无法更新行

将CLOUD_FIRESTORE程序包更新到版本4.13.0(到Windows)时出现问题

Flutter API请求BadState响应

Flutter Android Google Sign-in error:ApiException:10在确认软件包名称、SHA-1 fingerprint 和设置同意页面后出现异常

Flutter守护进程无法启动

Flutter类调用另一个类中的另一个方法失败

Flutter Xcode 15 错误(Xcode):DT_TOOLCHAIN_DIR 无法用于判断 LIBRARY_SEARCH_PATHS

为什么我的图标的一半在定位小部件中消失了?

为 flutter app/firebase 保存 chatgpt api-key 的正确方法是什么

来自服务器的数据未分配给变量

Flutter - firebase_auth_web 5.4.0 依赖于 intl ^0.17.0 但我必须使用更高版本 (intl ^0.18.0)

如何正确地将 PageView 设置为 AppBar 的标题?

在 flutter 中使用 Firebase 实时数据库数据创建无限滚动效果

Flutter:如何将列表转换为字符串?

Flutter 小部件中的视图和逻辑分离