Перевод в произвольную систему счисления
Добавлено: 11 апр 2008, 11:38
Первая функция переводит десятичное число x в число в системе счисления с основанием base. Вторая - обратно. Работает только с положительными числами. base должна быть в промежутке 2..36
Код: Выделить всё
function from_decimal(x, base: longint): string;
var res:string; a:integer;
begin
if x=0 then begin
from_decimal:='0';
exit;
end;
res:='';
while x>0 do
begin
a:=x mod base;
if a<10 then res:=chr(ord('0')+a)+res
else res:=chr(ord('A')+a-10)+res;
x:=x div base;
end;
from_decimal:=res;
end;
function to_decimal(x:string; base: longint): longint;
var res: longint;
begin
res:=0;
while x<>'' do
begin
res:=res*base;
if x[1] in ['0'..'9'] then res:=res+ord(x[1])-ord('0')
else res:=res+ord(upcase(x[1]))-ord('A')+10;
delete(x,1,1);
end;
to_decimal:=res;
end;