5,388
社区成员
发帖
与我相关
我的任务
分享
//这是一个函数
function StrTOBcd(Str: string): string;
var
aa, n,i, Bytes: Byte;
Odd: boolean;
hex1,Hex: string;
begin
str := trim(str); //去空格和回车等特殊字符
str := stringreplace(str, ' ', '', [rfReplaceAll]);
Result := '';
Odd := Length(Str) mod 2 = 1;
Bytes := (Length(Str) + 1) div 2;
if Odd then Str := Str + '0';
for i := 0 to Bytes - 1 do
begin
Hex := Copy(Str, i * 2 + 1, 2);
//ShowMessage(chr(01));
Result := Result + chr(HexToInt(Hex));
end;
end;
//另一个
function BcdTOstr(ss: string): string;
var
i: integer;
s, s2: string;
begin
for i := 1 to length(ss) do
begin
s2 := ss[i];
s := s + BCD2Str(pchar(s2), 0, 2);
end;
result := s;
end;
function BCD2Str(P: PChar; Pos, Len: integer): string;
var
i: integer;
strResult: string;
cRead: Byte;
cReadH, cReadL: Byte;
bOdd: boolean;
begin
strResult := '';
if (Len mod 2) = 1 then
begin
Len := (Len + 1) div 2;
bOdd := true;
end else begin
Len := Len div 2;
bOdd := false;
end;
for i := 0 to Len - 1 do
begin
cRead := Ord(P[pos + i]);
cReadH := (cRead and $F0) shr 4;
strResult := strResult + IntToHex(cReadH, 1);
if (i = Len - 1) and bOdd then continue;
cReadL := cRead and $0F;
strResult := strResult + IntToHex(cReadL, 1);
end;
BCD2Str := strResult;
end;