字串通訊協議解析函式

2021-09-07 22:37:45 字數 3644 閱讀 2503

unit hutil32;

//提供大量的輔助函式

inte***ce

uses

classes, sysutils, wintypes, winprocs, graphics, messages, dialogs;

type

str4096 = array [0..4096] of ansichar;

str256 = array [0..256] of ansichar;

//名稱、值對照表

tynametable = record

name: string;

varl: longint;

end;

tlrect = record

left, top, right, bottom: longint;

end;

// 取 searchafter, arrestbefore之間的字串

function  arreststringex (source, searchafter, arrestbefore: ansistring; var arreststr: ansistring): ansistring;

// 獲取指定字串中以指定分割符結尾的子串

function  getvalidstr3 (str: ansistring; var dest: ansistring; const divider: array of ansichar): ansistring;

implementation

function arreststringex (source, searchafter, arrestbefore: ansistring; var arreststr: ansistring): ansistring;

varsrclen: integer;

gooddata: boolean;

i, n: integer;

begin

arreststr := '';

if source = '' then begin

result := '';

exit;

end;

trysrclen := length (source);

gooddata := false;

if srclen >= 2 then

if source[1] = searchafter then begin

source := copy (source, 2, srclen-1);

srclen := length (source);

gooddata := true;

end else begin

n := pos (searchafter, source);

if n > 0 then begin

source := copy (source, n+1, srclen-(n));

srclen := length(source);

gooddata := true;

end;

end;

if gooddata then begin

n := pos (arrestbefore, source);

if n > 0 then begin

arreststr := copy (source, 1, n-1);

result := copy (source, n+1, srclen-n);

end else begin

result := searchafter + source;

end;

end else begin

for i:=1 to srclen do begin

if source[i] = searchafter then begin

result := copy (source, i, srclen-i+1);

break;

end;

end;

end;

except

arreststr := '';

result := '';

end;

end;

//獲取指定字串中以指定分割符結尾的子串

function getvalidstr3 (str: ansistring; var dest: ansistring; const divider: array of ansichar): ansistring;

const

buf_size = 20480; //$7fff;

varbuf: array[0..buf_size] of ansichar;

bufcount, count, srclen, i, arrcount: longint;

ch: ansichar;

label

catch_div;

begin

trysrclen := length(str);

bufcount := 0;

count := 1;

if srclen >= buf_size-1 then begin

result := '';

dest := '';

exit;

end;

if str = '' then begin

dest := '';

result := str;

exit;

end;

arrcount := sizeof(divider) div sizeof(ansichar);

while true do begin

if count <= srclen then begin

ch := str[count];

for i:=0 to arrcount- 1 do

if ch = divider[i] then

goto catch_div;

end;

if (count > srclen) then begin

catch_div:

if (bufcount > 0) then begin

if bufcount < buf_size-1 then begin

buf[bufcount] := #0;

dest := string (buf);

result := copy (str, count+1, srclen-count);

end;

break;

end else begin

if (count > srclen) then begin

dest := '';

result := copy (str, count+2, srclen-1);

break;

end;

end;

end else begin

if bufcount < buf_size-1 then begin

buf[bufcount] := ch;

inc (bufcount);

end;

end;

inc (count);

end;

except

dest := '';

result := '';

end;

end;

end.

通訊協議解析

異或校驗小工具 或者計算機計算也行 將乙個數寫成兩個位元組 將乙個數寫成四個位元組 將四個位元組合成乙個數 將兩個位元組合成乙個數 標頭檔案mainwindow.h static uchar crc 8 qbytearray crcdata,int beginindex,int crcdatalen...

字串 通訊錄排序

7 5 通訊錄排序 20 分 輸入n個朋友的資訊,包括姓名 生日 號碼,本題要求編寫程式,按照年齡從大到小的順序依次輸出通訊錄。題目保證所有人的生日均不相同。輸入第一行給出正整數n 10 隨後n行,每行按照 姓名 生日 號碼 的格式給出一位朋友的資訊,其中 姓名 是長度不超過10的英文本母組成的字串...

SPI通訊協議(SPI匯流排)解析

1 什麼是spi?spi是序列外設介面 serial peripheral inte ce 的縮寫。是 motorola 公司推出的一 種同步序列接 術,是一種高速的,全雙工,同步的通訊匯流排。2 spi優點 支援全雙工通訊 通訊簡單 資料傳輸速率塊 3 缺點 沒有指定的流控制,沒有應答機制確認是否...