Program RadixValue;
(* convert number string to real number, any base from 2 until 36
For more information look at the Val procedure F1 help *)
Var Radix : Longint;
Procedure RadixVal(S : String;Var r : Real;Var Err : Integer);
Var DpL : Real;
P : Integer;
Sign : Real;
L : Longint;
Begin
r := 0.0; {preset}
DpL := Radix;
Sign := 1.0;
Err := 0;
For P := 1 to length(S) do
Begin
S[P] := Upcase(S[P]); {convert 'a'..'z' to 'A'..'Z'}
Case S[P] of
'-' : if Sign > 0 then
Sign := -1.0 {was the first hyphen in the string, ok}
else
Begin
Err := P; {multiple leading -}
Exit;
End;
'.' : if DpL > 1.0 then
DpL := 1.0/Radix {from now on: right of the decimal point}
else
Begin
Err := P; {multiple Dp error}
Exit;
End;
'0'..'9',
'A'..'Z': Begin
if S[P] <= '9' then
L := Ord(S[P])-Ord('0') {'0'..'9' -> 0..9}
else
L := Ord(S[P])-Ord('A')+10; {'A'..'Z' -> 10..35}
if L < Radix then
Begin
if DpL > 1.0 then {no Dp encountered yet}
r := r*Radix + 1.0*L {increase the exponent}
else
Begin {we are behind the Dp}
r := r + DpL*L; {append at the right end of the ###}
DpL := DpL/Radix;
End;
End
else
Begin
Err := P; {the cipher was too high for the selected base}
Exit;
End;
End;
else
Begin {any other bad character}
Err := P;
Exit;
End;
End;{case}
End;
r := r*Sign; {finally}
End;
Var Test : String;
Rslt : Real;
ErrLoc : Integer;
Begin
Radix := 10;
Test := '-1234.5678';
RadixVal(Test,Rslt,ErrLoc);
writeln(ErrLoc,Rslt:20:10);
Radix := 2;
Test := '-100000000.100001';
RadixVal(Test,Rslt,ErrLoc);
writeln(ErrLoc,Rslt:20:10);
Radix := 16;
Test := '-AFFE.BAC0';
RadixVal(Test,Rslt,ErrLoc);
writeln(ErrLoc,Rslt:20:10);
End.
{Franz Glaser, Aug.1-1998 meg-glaser@eunet.at
http://www.geocities.com/SiliconValley/2926/tp.html}
               (
geocities.com/~franzglaser)