function TForm1.f(ID: string):string; const W:array [1..18] of integer = (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1); A:array [0..10] of char = ('1','0','x','9','8','7','6','5','4','3','2'); var i, j, S: integer; NewID: string; begin if Length(ID) <> 15 then result:= '' else begin NewID:= ID; Insert('19', NewID, 7); S:= 0; try for i:=1 to 17 do begin j:= StrToInt(NewID[i]) * W[i]; S:= S + j; end; except result:= ''; exit; end; S:= S mod 11; Result:= NewID + A[S]; end; end; copy过来的18-15
//15位转18位函数 function TForm1.ID15To18(OldID:string):string ; var i:Integer; num:Integer; code:string; strtemp:string; pvalue:string; begin num:=0; strtemp:=Leftstr(OldID,6)+ '19'+Rightstr(OldID,9); for i:=18 downto 2 do begin pvalue:=floattostr(power(2,i-1)); num:=num +(strtoint(pvalue) mod 11) * strtoint(Midstr(strtemp, 19 - i, 1)); end; num:=num mod 11; case num of 0: code:='1'; 1: code:='0'; 2: code:='X'; else code:=inttostr(12-num); end; Result:=strtemp+code; end;肯定好用的函数
const
W:array [1..18] of integer = (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1);
A:array [0..10] of char = ('1','0','x','9','8','7','6','5','4','3','2');
var
i, j, S: integer;
NewID: string;
begin
if Length(ID) <> 15 then
result:= ''
else begin
NewID:= ID;
Insert('19', NewID, 7);
S:= 0;
try
for i:=1 to 17 do begin
j:= StrToInt(NewID[i]) * W[i];
S:= S + j;
end;
except
result:= '';
exit;
end;
S:= S mod 11;
Result:= NewID + A[S];
end;
end;
copy过来的18-15
function TForm1.ID15To18(OldID:string):string ;
var
i:Integer;
num:Integer;
code:string;
strtemp:string;
pvalue:string;
begin
num:=0;
strtemp:=Leftstr(OldID,6)+ '19'+Rightstr(OldID,9);
for i:=18 downto 2 do
begin
pvalue:=floattostr(power(2,i-1));
num:=num +(strtoint(pvalue) mod 11) * strtoint(Midstr(strtemp, 19 - i, 1));
end;
num:=num mod 11;
case num of
0: code:='1';
1: code:='0';
2: code:='X';
else
code:=inttostr(12-num);
end;
Result:=strtemp+code;
end;肯定好用的函数