代码如下
function GetLocalKID(guidstr: string; StrLength: Integer): string;
var
CID, Res : string;
L, I1, I2, I3, K1, K2: Integer;
const
F = '0123456789ABCDEF';
T = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-';
begin
CID := guidstr;
CID := ReplaceStr(CID, '{', '');
CID := ReplaceStr(CID, '}', '');
CID := ReplaceStr(CID, '-', '') + '0';
if StrLength = 33 then
begin
Result := CID;
end
else if StrLength = 22 then
begin
Res := '';
L := 1;
while L < 33 do
begin
I1 := Pos(Copy(CID, L, 1), F);
I2 := Pos(Copy(CID, L + 1, 1), F);
I3 := Pos(Copy(CID, L + 2, 1), F);
K1 := I1 * 4 + Trunc(I2 / 16);
K2 := (I2 mod 16) * 4 + I3;
Res := Res + Copy(T, K1, 1) + Copy(T, K2, 1);
L := L + 3;
end;
L := Length(Res);
while L < 22 do
begin
Res := Res + '0';
L := L + 1;
end;
Result := Res;
end;
end;
2.使用
function LocalKID(StrLength: Integer = 22): string;
var
tmpGUID : TGUID;
begin
CreateGUID(tmpGUID);
Result := GetLocalKID(GUIDToString(tmpGUID), StrLength);
end;