January 24, 2014

From binary to decimal

The program transforms the number from binary to decimal.
Programming language: FreePascal.

Code:

Program Bin2Dec;
Uses crt;

Function BinToDec(s:string):integer;
Var
 x,i:integer;
Begin
 x:=0;
 For i:=1 to length(s) do
 x:=(x+ord(s[i])-$30)shl 1;

 BinToDec:=x shr 1;
End;

Var
 s:string;

Begin
 ClrScr;
 Writeln('Bin: ');
 Readln(s);
 Writeln('');
 Writeln('Dec: ');
 Writeln(BinToDec(s));
 Readln;
End.

No comments:

Post a Comment