//***************************************************************************//
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils,
Dialogs;
const maxlen=100; { max length of the number }
type
HP=record
len: integer; { length of the number }
s: array[1..maxlen] of integer;
{ s[1] is the lowest position
s[len] is the highest position }
end;
procedure Divide(a: hp; b: integer; var c: hp; var d: integer);
{ c:=a div b ; d:=a mod b }
var i,len:integer;
begin
fillchar(c,sizeof(c),0);
len:=a.len;
d:=0;
for i:=len downto 1 do { from high to low }
begin
d:=d*10+a.s[i];
c.s[i]:=d div b;
d:=d mod b;
end;
while(len>1) and (c.s[len]=0) do dec(len);
c.len:=len;
end;
function HPMod(Dividend, Divisor: string): string;
var
i: integer;
x, y: HP;
ri: integer;
begin
x.len:= length(Dividend);
for i:=1 to x.len do {change string to HP}
x.s[i]:= ord(Dividend[x.len + 1 - i]) - ord('0');
Divide(x, StrToInt(Divisor), y, ri);
result:= IntToStr(ri);
end;
function HPDiv(Dividend, Divisor: string): string;
var
i: integer;
x, y: HP;
ri: integer;
begin
x.len:=length(Dividend);
for i:=1 to x.len do {change string to HP}
x.s[i]:=ord(Dividend[x.len+1-i]) - ord('0');
Divide(x, StrToInt(Divisor), y, ri);
Setlength(result, y.len);
for i:= y.len downto 1 do {change HP to string}
result[i]:= chr(y.s[y.len - i + 1] + ord('0'));
end;
function HPIntToHex(HPInt: string): string;
const
stri:string = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var
i, j: integer;
a: array[0..maxlen-1] of string;
begin
i:= 0;
while (HPInt <> '0') do
begin
a[i]:= HPMod(HPInt, '16');
HPInt:= HPDiv(HPInt, '16');
inc(i);
end;
SetLength(result, i + 1);
for j:= i -1 downto 0 do
result[j + 1]:= stri[StrToInt(a[i - j - 1])+1];
end;
begin
//测试代码:
ShowMessage(HPIntToHex('18446744073709551614'));
end.
//***************************************************************************//