Реализация _IdToStr и _StrToID в Delphi
///////////////////////////////////////////////////////////////////////
function _IdToStr(i: Integer): String;
var s: String;
var ost: Integer;
var c: char;
const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
begin
if i>=2147483647 then Raise Exception.Create('Ожидается число < 2147483647');
if i<0 then Raise Exception.Create('Ожидается значение >=0');
s:='';
repeat
ost:=i mod 36;
i:=i div 36;
c:=chars[ost+1];
s:=c+s;
until i=0;
while Length(s)<6 do begin
s:=' '+s;
end;
Result:=s;
end;
///////////////////////////////////////////////////////////////////////
function _StrToId(s: String): Integer;
var
c: char;
i: Integer;
p: Integer;
const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
begin
Result:=0;
s:=trim(s);
if Length(s)>6 then Raise Exception.Create('Ожидается строка не длиннее 6 символов: '+s);
for i:=1 to Length(s) do begin
c:=s[i];
p:=pos(c, chars)-1;
if p<0 then Raise Exception.Create('Ожидается символ '+chars);
Result:=Result*36;
Result:=Result+p;
end;
if Result<0 then Raise Exception.Create('На входе слишком большое значение: '+s);
end;
|