///////////////////////////////////////////////////////////
// 从文本字串中分解出16进制的数据,返回这些8位数据组成的字串
// 文本字串分解规则:
// 1.数据是用16进制表示的8位数据(如FC,3D等)
// 2.每个数据只取最后两个16进制字符,如1F3D只取3D,前面多余部分抛弃
// 3.每个数据之间用空格或回车换行等格式符号隔开
///////////////////////////////////////////////////////////
function GetDataFromText(str: string): string;
var
i, p1, p2: Integer;
begin
Result := '';
while str <> '' do
begin
i := 0;
p1 := Pos(' ', str);
p2 := Pos(#13#10, str);
if p1 = 1 then // 空格在最前面
begin
Delete(str, 1, 1);
continue;
end;
if p2 = 1 then // #13#10在最前面
begin
Delete(str, 1, 2);
continue;
end;
if (p1 = 0) and (p2 = 0) and (str <> '') then // 都没有找到,结束
begin
i := StrToIntDef('$' + str, 0);
Delete(str, 1, Length(str));
end;
if ((p1 > 0) and (p2 = 0)) or // 找到空格
((p1 > 0) and (p2 > 0) and (p1 < p2)) then // 或都找到,但空格在前
begin
i := StrToIntDef('$' + Copy(str, 1, p1 - 1), 0);
Delete(str, 1, p1);
end;
if ((p1 = 0) and (p2 > 0)) or // 找到#13#10
((p1 > 0) and (p2 > 0) and (p1 > p2)) then // 或都找到,但空格在后
begin
i := StrToIntDef('$' + Copy(str, 1, p2 - 1), 0);
Delete(str, 1, p2 + 1);
end;
Result := Result + Chr(i); // 8位数据转为字符
end; // while
end;
//写卡
procedure Tfrm_ICAccredit.btn_writeCardClick(Sender: TObject);
var
S: string;
Buf: array[0..$16] of Byte;
I: Integer;
begin //TODO:
FillChar(Buf, SizeOf(Buf), #0);
//扇区,块
Buf[0] := HexToInt(cbx_Writefan.Text) *4 + HexToInt(cbx_WriteBlock.Text);
//检查扇区块是否可写
I := ((Buf[0] + 1) mod 4);
if Buf[0] = 0 then begin
AppMsgBox(Format('该块[%2.2x]不能写入数据。', [Buf[0]]));
Exit;
end;
if I = 0 then begin
AppMsgBox(Format('该块[%2.2x]不能写入数据。', [Buf[0]]));
Exit;
end;
if CheckEditIsEmpty( edt_writePw) then Exit;
//检查数据有效性
if CheckEditIsEmpty( edt_writeData) then Exit;
//密码
S := GetDataFromText(edt_writePw.Text);
for I := 1 to Length(S) do
Buf := Ord(S);
S := GetDataFromText(edt_writeData.Text);
for I := 1 to Length(S) do
Buf[I + 6] := Ord(S);
try
if self.BaseComm.SendWriteICCardCmd($0F, Buf) then
self.UpdateLog('发送写卡数据命令成功', clGray)
else Self.UpdateLog('发送写卡数据命令成功失败', clGray);
except
on E: Exception do
self.UpdateLog(E.Message, clRed);
end;
end;
function BCDToInt(HighHex, LowHex: Integer): Integer;
begin
try
Result := StrToInt(IntToHex(HighHex, 1) + IntToHex(LowHex, 1));
except
Result := 0;
end;
end;
//取Word中的一个Byte类型转换成Byte Low
function GetBCDByLow(D: Word): Byte;
var
I: integer;
S: string;
M: string;
begin
Result := 0;
if D > 9999 then
raise Exception.Create('不能大于9999.');
S := IntToStr(D);
I := Length(S);
I := Length(S);
case I of
1, 2: M := Copy(S, 1, 2);
3: M := Copy(S, 2, 2);
4: M := Copy(S, 3, 2);
end;
Result := HexToInt(M);
end;
//取Word中的一个Byte类型转换成Byte High
function GetBCDByHigh(D: Word): Byte;
var
I: integer;
S: string;
M: string;
begin
Result := 0;
if D > 9999 then
raise Exception.Create('不能大于9999.');
S := IntToStr(D);
I := Length(S);
if I <=2 then Exit; //高位是0
if I = 4 then
M := Copy(S, 1, 2)
else M := Copy(S, 1, 1);
Result := HexToInt(M);
end;
//将array of Byte 格式化为Hex显示,2.2x格式:FF FE FF
function ArrayByteToHex(Buf: array of Byte; Count: Integer = 0): string;
var
I: Integer;
Len: Integer;
begin
if Count = 0 then
Len := High(Buf)
else Len := Count - 1;
Result := '';
for I := 0 to Len do
Result := Result + Format('%2.2x', [Buf]) + #32;
end;
//将String 格式化为Hex显示,2.2x格式:FF FE FF
function StringToHex(const S: string): string;
var
I: Integer;
begin
Result := '';
for I := 1 to Length(S) do
Result := Result + Format('%2.2x', [Ord(S)]) + #32;
end;
//十六进制字符串转换成十进制整型
function HexToInt(Astr: string): Integer;
function TransChar(AChar: Char): Integer;
begin
if AChar in ['0'..'9'] then
Result := Ord(AChar) - Ord('0')
else
Result := 10 + Ord(AChar) - Ord('A');
end;
var
I: Integer;
C: Integer;
ISqr: Integer;
begin
Result := 0;
for I := 1 to Length(Astr) do
begin
ISqr := Trunc(IntPower(16, Length(Astr) - I));
C := TransChar(AStr);
Result := Result + ISqr * C;
end;
end;