Chương trình ví dụ:
Mã: Chọn tất cả
Moi ban nhap vao mot so o he thap phan: 5
So nhi phan tuong ung la: 101
Mã: Chọn tất cả
Program H10_16;
Uses Crt, SysUtils;
Var N: LongInt;
Re: String;
Begin
ClrScr;
ReadLn(N);
Re:= '';
While N > 0 Do Begin
Case (N Mod 16) Of
10: Re:= 'A' + Re;
11: Re:= 'B' + Re;
12: Re:= 'C' + Re;
13: Re:= 'D' + Re;
14: Re:= 'E' + Re;
15: Re:= 'F' + Re;
Else
Re:= IntToStr(N Mod 16) + Re;
End;
N:= N Div 16;
End;
WriteLn(Re);
ReadLn;
End.
Mã: Chọn tất cả
Program H16_10;
Uses Crt, SysUtils;
Function Mu(a, b: Integer): LongInt;
Begin
Mu:= 1;
While b > 0 Do Begin
Mu:= Mu*a;
Dec(b);
End;
Exit(Mu);
End;
Var hex: String;
i: Integer;
Re: LongInt;
Begin
ClrScr;
ReadLn(hex);
Re:= 0;
For i:= Length(hex) DownTo 1 Do
Case hex[i] Of
'A': Re:= Re + 10*Mu(16, Length(hex)-i);
'B': Re:= Re + 11*Mu(16, Length(hex)-i);
'C': Re:= Re + 12*Mu(16, Length(hex)-i);
'D': Re:= Re + 13*Mu(16, Length(hex)-i);
'E': Re:= Re + 14*Mu(16, Length(hex)-i);
'F': Re:= Re + 15*Mu(16, Length(hex)-i);
Else
Re:= Re + StrToInt(hex[i])*Mu(16, Length(hex)-i);
End;
WriteLn(Re);
ReadLn;
End.
Mã: Chọn tất cả
Program H10_2;
Uses Crt, SysUtils;
Var N: Integer;
Re: String;
Begin
ClrScr;
ReadLn(N);
Re:= '';
While N > 0 Do Begin
Re:= IntToStr(N Mod 2) + Re;
N:= N Div 2;
End;
WriteLn(Re);
ReadLn;
End.
Mã: Chọn tất cả
Program H2_10
Uses Crt;
Function Mu(a, b: Integer): Integer;
Begin
Mu:= 1;
While b > 0 Do Begin
Mu:= Mu*a;
Dec(b);
End;
Exit(Mu);
End;
Var bin: String;
i, Re: Integer;
Begin
ClrScr;
ReadLn(bin);
Re:= 0;
For i:= Length(bin) DownTo 1 Do
If bin[i] = '1' Then Re:= Re + Mu(2, Length(bin)-i);
WriteLn(Re);
ReadLn;
End.