delphi - Indy UDP Read Contents of Adata -


i using indy udp server read string of data. however, not know how work adata parameter. can around converting binary using bytetobin function below , convert hex using bintohex1. feel stupid , works slow. know how can directly results without 2 conversions?

thanks!!

here codes:

procedure tform1.idudpserver1udpread(athread: tidudplistenerthread;   const adata: tidbytes; abinding: tidsockethandle); var   buf: tidbytes;   buffer: string;   data_received: string; begin   if bytestostring(adata) <> 'hello'   begin     buffer := hextostring('00');     setlength(buf, length(buffer));     copytidstring(buffer, buf, 0);     abinding.sendto(abinding.peerip, abinding.peerport, buf);    data_received := bintohex1(bytetobin(adata[1]) + bytetobin(adata[2]) +     bytetobin(adata[3]) + bytetobin(adata[4]) + bytetobin(adata[5]) +     bytetobin(adata[6]) + bytetobin(adata[7]) + bytetobin(adata[8]) +     bytetobin(adata[9]) + bytetobin(adata[10]) + bytetobin(adata[11]) +     bytetobin(adata[12]));    end;   memo1.lines.add(data_received);   memo1.gototextend; end;  function tform1.bytetobin(abyte: byte): string; const   c10: array [boolean] of char = ('0', '1'); var   eloop1: byte; begin   setlength(result, 8);   eloop1 := 7 downto 0     result[8 - eloop1] := c10[(abyte , (1 shl eloop1)) <> 0]; end;  function tform1.bintohex1(binstr: string): string; const   binarray: array [0 .. 15, 0 .. 1] of string = (('0000', '0'), ('0001', '1'),     ('0010', '2'), ('0011', '3'), ('0100', '4'), ('0101', '5'), ('0110', '6'),     ('0111', '7'), ('1000', '8'), ('1001', '9'), ('1010', 'a'), ('1011', 'b'),     ('1100', 'c'), ('1101', 'd'), ('1110', 'e'), ('1111', 'f')); var   error: boolean;   j: integer;   binpart: string; begin   result := '';    error := false;   j := 1 length(binstr)     if not(binstr[j] in ['0', '1'])     begin       error := true;       showmessage('this not binary number');       break;     end;    if not error   begin     case length(binstr) mod 4 of       1:         binstr := '000' + binstr;       2:         binstr := '00' + binstr;       3:         binstr := '0' + binstr;     end;      while length(binstr) > 0     begin       binpart := copy(binstr, length(binstr) - 3, 4);       delete(binstr, length(binstr) - 3, 4);       j := 1 16         if binpart = binarray[j - 1, 0]           result := binarray[j - 1, 1] + result;     end;   end; end; 

your code unnecessarily complex. indy has many functions in idglobal unit working tidbytes data. example, can simplify example following:

procedure tform1.idudpserver1udpread(athread: tidudplistenerthread;   const adata: tidbytes; abinding: tidsockethandle); begin   if bytestostring(adata) <> 'hello' begin     abinding.sendto(abinding.peerip, abinding.peerport, tobytes(byte(0)));   end;   memo1.lines.add(tohex(adata));   memo1.gototextend; end; 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -