字符串转换成16进制字符串
function StrToHexStr(const S:string):string; 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;
//字符串转换为2进制字符串
function StrToBinStr(const S:string):string; var I:Integer; begin Result:=''; for I:=1 to Length(S) do begin if I=1 then Result:=BinStrArray[Ord(S[1])] else Result:=Result+' '+BinStrArray[Ord(S[I])]; end; end;
//Byte型转换为二进制字符串
function ByteToBinStr(const B:Byte):string; var I:Integer; begin Result:=''; for I:=7 downto 0 do Result:=Result+Chr(((B shr I) and 1)+48); end;
//二进制字符串转成数字
function BinStrToByte(const S:string):Byte; var S1:string[8]; L:Integer; I:Integer; begin S1:='00000000'; L:=min(8,Length(S)); for I:=L downto 1 do if S[I]='1' then S1[8+I-L]:=S[I] else S1[8+I-L]:='0'; Result:=0; for I:=0 to 7 do Result:=Result+(((Ord(S1[I+1])-48) shl (7-I))); end;
//T2Byte类型转换成Word类型
function T2ByteToWord(const T:T2Byte;const LowFront:Boolean=False):Word; var a1,a2:Byte; begin if LowFront then begin a1:=T[1]; a2:=T[0]; end else begin a1:=T[0]; a2:=T[1]; end; Result:=a1; Result:=(Result shl 8) + a2; end;
//Word类型转换成T2Byte类型
function WordToT2Byte(const aNum:Word;const LowFront:Boolean=False):T2Byte; var a1,a2:Byte; begin a1:=aNum; a2:=aNum shr 8; if LowFront then begin Result[0]:=a1; Result[1]:=a2; end else begin Result[0]:=a2; Result[1]:=a1; end; end;
//Byte转换成BCD码
function ByteToBCDByte(const B:Byte;const aType:TBCDType=bt8421):Byte; var B1,B2:Byte; function T2421(const P:Byte):Byte; begin if P<5 then Result:=P else Result:=P+6; end; begin if B>99 then Result:=0 else begin B1:=B div 10; B2:=B mod 10; case aType of bt8421:begin Result:=(B1 shl 4)+B2; end; bt2421:begin Result:=(T2421(B1) shl 4)+T2421(B2); end; btR3:begin Result:=((B1+3) shl 4)+(B2+3) end; else Result:=(B1 shl 4)+B2; end; end; end;
//BCD转成Byte型
function BCDByteToByte(const B:Byte;const aType:TBCDType=bt8421):Byte; var B1,B2:Byte; function Ts2421(const P:Byte):Byte; begin if P<5 then Result:=P else Result:=P-6; end; begin B1:=(B and $F0) shr 4; B2:=B and $0F; case aType of bt8421: begin Result:=B1*10+B2; end; bt2421: begin Result:=(Ts2421(B1)*10)+Ts2421(B2); end; btR3: begin Result:=((B1-3)*10)+(B2-3) end; else Result:=B1*10+B2; end; end;
//BCD转成Byte型 FF-->FF 8421码
function BCDByteToByteFF(const B:Byte;Auto:Boolean=True):Byte; var B1,B2:Byte; begin if (B=$FF)or(Auto and (B>=$AA)) then Result:=B else begin B1:=(B and $F0) shr 4; B2:=B and $0F; Result:=B1*10+B2; end; end;
//Byte转换成BCD码 FF-->FF 8421码
function ByteToBCDByteFF(const B:Byte;Auto:Boolean=True):Byte; var B1,B2:Byte; begin if (B=$FF)or(Auto and (B>=$AA)) then Result:=B else if B>99 then Result:=0 else begin B1:=B div 10; B2:=B mod 10; Result:=(B1 shl 4)+B2; end; end;
字符串转换成16进制字符串
//16进制字符串转换成字符串
//字符串转换成10进制字符串//字符串转换成相应Byte
//Byte转换成相应字符串
//字符串转换成相应BCDByte
//BCDByte转换成相应字符串
//集合形式转换成数组
//字符串转换为2进制字符串
//Byte型转换为二进制字符串
//二进制字符串转成数字
![]()
//T2Byte类型转换成Word类型
//Word类型转换成T2Byte类型
//Byte转换成BCD码
//BCD转成Byte型
//BCD转成Byte型 FF-->FF 8421码
function ByteToBCDByteFF(const B:Byte;Auto:Boolean=True):Byte; var B1,B2:Byte; begin if (B=$FF)or(Auto and (B>=$AA)) then Result:=B else if B>99 then Result:=0 else begin B1:=B div 10; B2:=B mod 10; Result:=(B1 shl 4)+B2; end; end;
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)