连接TCP套接字服务器并发送请求。并且服务器以字节数组发送响应。如何在dart中读取字节数组数据。
function varargout = untitled(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = untitled_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
h = imshow('hestain.png', 'Parent', handles.axes1);
hp = impixelinfoval(gcf, h);
addlistener(hp, 'String', 'PostSet', @mycallback) % Works
% addlistener(hp, 'String', 'PostSet', @mycallback1(hObject, handles)) % Does not work. I would like to pass both hObject and handles to mycallback function.
function mycallback(src, evt)
disp(evt.AffectedObject.String)
function mycallback1(src, evt, hObject, handles)
disp(evt.AffectedObject.String)
% Create a variable and store it in the handles
handles.pixelcoord = evt.AffectedObject.String;
% Update handles
guidata(hObject, handles)
答案 0 :(得分:1)
这取决于将数据类型编码为字节。假设它是String
然后,您可以使用dart:convert
库。
import 'dart:convert' show utf8;
final decoded = utf8.decode(data);
答案 1 :(得分:1)
很明显,这些字节中有一个消息结构。您给出两个消息示例:
[255,12,0,11,0,9,34,82,69,70,84,65,72,73,76]
和
[255、20、0、11、0、0、0、15、80、82、69、77、84、65、72、73、76、45、53、53、57、55、48]
两者都以255开头,后跟两个或三个小端16位字(12和11),(20、11和0),然后是字符串,其长度编码在前导字节中。如果希望与另一个系统进行互操作,则确实需要协议规范。
假设我正确猜测了该结构,此代码
main() {
Uint8List input = Uint8List.fromList([
255,
20,
0,
11,
0,
0,
0,
15,
80,
82,
69,
77,
84,
65,
72,
73,
76,
45,
53,
53,
57,
55,
48
]);
ByteData bd = input.buffer.asByteData();
print(bd.getUint16(1, Endian.little)); // print the first short
print(bd.getUint16(3, Endian.little)); // and the second
print(bd.getUint16(5, Endian.little)); // and the third
int stringLength = input[7]; // get the length of the string
print(utf8.decode(input.sublist(8, 8 + stringLength))); // decode the string
}
产生
20
11
0
PREMTAHIL-55970
符合预期