字符串转换成16进制字符串
实例 Function StrToHexStr(const S:string):string; //字符串转换成16进制字符串 var I:Integer; begin for I:=1 to Length(S) do begin if I=1 then Result:=IntToHex(Ord(S[1]),2) else Result:=Result+' '+IntToHex(Ord(S[I]),2); end; end;
16进制字符串转换成字符串
实例 Function HexStrToStr(const S:string):string; var t:Integer; ts:string; M,Code:Integer; begin t:=1; Result:=''; while t<=Length(S) do begin while not (S[t] in ['0'..'9','A'..'F','a'..'f']) do inc(t); if (t+1>Length(S))or(not (S[t+1] in ['0'..'9','A'..'F','a'..'f'])) then ts:='$'+S[t] else ts:='$'+S[t]+S[t+1]; Val(ts,M,Code); if Code=0 then Result:=Result+Chr(M); inc(t,2); end; end;
字符串转换成10进制字符串
实例 Function StrToTenStr(const S:string):string; var I:Integer; begin for I:=1 to Length(S) do begin if I=1 then Result:=IntTostr(Ord(S[1])) else Result:=Result+' '+IntToStr(Ord(S[I])); end; end;
字符串转换成相应Byte
实例 Function StrToByteArray(const S:string):TarrayByte; var I:Integer; begin SetLength(Result,Length(S)); for I:=1 to Length(S) do Result[I-1]:=Ord(S[I]); end;
Byte转换成相应字符串
实例 Function ByteArrayToStr(const aB:TarrayByte):string; var I:Integer; begin SetLength(Result,High(aB)+1); for I:=0 to High(aB) do Result[I+1]:=Chr(aB[I]); end;
字符串转换成相应BCDByte
实例 Function StrToBcdByteArray(const S:string; const Rotate:Boolean=False):TarrayByte; var ts:string; I:Integer; begin if Odd(Length(S)) then ts:='0'+S else ts:=S; SetLength(Result,Length(ts) div 2); for I:=0 to High(Result) do begin if Rotate then Result[High(Result)-I]:=StrToInt('$'+ts[2*I+1]+ts[2*I+2]) else Result[I]:=StrToInt('$'+ts[2*I+1]+ts[2*I+2]); end; end;
BCDByte转换成相应字符串
实例 Function BcdByteArrayToStr(const aNum:TarrayByte; const Rotate:Boolean=False):string; var I:Integer; t:string; begin SetLength(Result,2*(High(aNum)+1)); for I:=0 to High(aNum) do begin t:=IntToHex(aNum[I],2); if Rotate then begin Result[2*(High(aNum)-I)+1]:=t[1]; Result[2*(High(aNum)-I)+2]:=t[2]; end else begin Result[2*I+1]:=t[1]; Result[2*I+2]:=t[2]; end; end; end;
集合形式转换成数组
函数
Function SetToByteArray(s:array of const):TarrayByte;
var
I:Byte;
begin
SetLength(Result,High(s)+1);
for I:=0 to High(s) do
Result[I]:=S[I].VInteger;
end;
Function SetToWordArray(s:array of const):TarrayWord;
var
I:Byte;
begin
SetLength(Result,High(s)+1);
for I:=0 to High(s) do
Result[I]:=S[I].VInteger;
end;