Yus Waroeng Software Blog’s Delphi Tutorial
fungsi faktorial
function Faktorial(n: Integer): LongInt;
var c: Integer;
begin
Result:=1;
if n=0 then Exit;
for c:=1 to n do
Result:=Result*c;
end;
Sumber : Mikrodata
Konversi Desimal ke Heksadesimal
HexString:=Format('%0x',DecValue);
Konversi Desimal ke Biner
function TForm1.DecToBinStr(N: Integer): string;
var
S: string;
i: Integer;
Negative: Boolean;
begin
if N<0 then Negative:=True;
N:=Abs(N);
for i:=1 to SizeOf(N)*8 do
begin
if N<0 then S:=S+'1'
else S:=S+'0';
N:=N shl 1;
end;
Delete(S,1,Pos('1',S)-1);
if Negative then S:='-'+S;
Result:=S;
end;
Konversi Biner ke Oktal
function BinToOct(BinStr: string): string;
var
i: Integer;
OctPart: Real;
LastPart, OctStr: string;
Error: Boolean;
begin
Error:=False;
OctStr:='';
case Length(BinStr) mod 3 of
1: BinStr:='00'+BinStr;
2: BinStr:='0'+BinStr;
end;
while Length(BinStr)>0 do
begin
LastPart:=Copy(BinStr, Length(BinStr)-2, 3);
Delete(BinStr, Length(BinStr)-2, 3);
OctPart:=0;
for i:=1 to 3 do
if not (LastPart[i] in ['0', '1']) then
begin
ShowMessage('This is not binary number');
Error:=True;
Break;
end
else
OctPart:=OctPart+StrToInt(LastPart[i])*Power(2, 3-i);
OctStr:=OctStr+FloatToStr(OctPart);
end;
Result:='';
if Error<>True then
begin
for i:=1 to Length(OctStr) do
Result:=Result+OctStr[Length(OctStr)-i+1];
while (Result[1]='0') and (Length(Result)>1) do
Delete(Result, 1, 1);
end;
end;
Konversi Biner Ke Heksadesimal
function BinToHex(BinStr: string): string;
const
BinArray: array[0..15, 0..1] of string =
(('0000', '0'), ('0001', '1'), ('0010', '2'), ('0011', '3'),
('0100', '4'), ('0101', '5'), ('0110', '6'), ('0111', '7'),
('1000', '8'), ('1001', '9'), ('1010', 'A'), ('1011', 'B'),
('1100', 'C'), ('1101', 'D'), ('1110', 'E'), ('1111', 'F'));
var
Error: Boolean;
j: Integer;
BinPart: string;
begin
Result:='';
Error:=False;
for j:=1 to Length(BinStr) do
if not (BinStr[j] in ['0', '1']) then
begin
Error:=True;
ShowMessage('This is not binary number');
Break;
end;
if not Error then
begin
case Length(BinStr) mod 4 of
1: BinStr:='000'+BinStr;
2: BinStr:='00'+BinStr;
3: BinStr:='0'+BinStr;
end;
while Length(BinStr)>0 do
begin
BinPart:=Copy(BinStr, Length(BinStr)-3, 4);
Delete(BinStr, Length(BinStr)-3, 4);
for j:=1 to 16 do
if BinPart=BinArray[j-1, 0] then
Result:=BinArray[j-1, 1]+Result;
end;
end;
end;
Konversi Biner ke Desimal
function Pow(i, k: Integer): Integer;
var
j, Count: Integer;
begin
if k>0 then j:=2
else j:=1;
for Count:=1 to k-1 do
j:=j*2;
Result:=j;
end;
function BinToDec(Str: string): Integer;
var
Len, Res, i: Integer;
Error: Boolean;
begin
Error:=False;
Len:=Length(Str);
Res:=0;
for i:=1 to Len do
if (Str[i]='0')or(Str[i]='1') then
Res:=Res+Pow(2, Len-i)*StrToInt(Str[i])
else
begin
MessageDlg('It is not a binary number', mtInformation, [mbOK], 0);
Error:=True;
Break;
end;
if Error=True then Result:=0
else Result:=Res;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if Edit1.Text<>'' then
Label2.Caption:=IntToStr(BinToDec(Edit1.Text));
end;
0 comments:
Post a Comment
Leave Your Comment Here!