home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / sorting.swg / 0022_RADIXQUE.PAS.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-05-28  |  1.8 KB  |  46 lines

  1. Turbo Pascal Optimization Contest # 51.
  2.  
  3. No tangible prizes, just some bragging rights, and a brain workout.
  4.  
  5. Assignment:  Write conversion routines similar to VAL and STR that can
  6.              handle a radix (base) of any number.  For example, below is
  7.              a straight Pascal Procedure to convert a String of any base
  8.              to a LongInt.  Can you improve the speed of this routine,
  9.              and Write a correspondingly fast routine to convert from a
  10.              LongInt to a String of any base?
  11.  
  12. Rules:       No rules.  BAsm is allowed, as long as the Functions are
  13.              readily Compilable without the use of TAsm.
  14.  
  15. Judging:     Code will be tested on a 386-40 on March 10th, by being
  16.              placed into a loop With no output, like this:
  17.  
  18.                StartTiming;
  19.                For Loop := 1 to 10000000 { ten million } do
  20.                  { Execute the test, no output }
  21.                WriteLn(StopTiming);
  22.  
  23. Ready, set, code!  Here's the sample...
  24.  
  25. (* This Function converts an ASCIIZ String S in base Radix to LongInt I
  26.  * With no verification of radix validity.   The calling Programmer is
  27.  * responsible For insuring that the radix range is 2 through 36.  The
  28.  * calling Programmer is also responsible For insuring that the passed
  29.  * String contains only valid digits in the specified Radix. No checking
  30.  * is done on the individual digits of a given String.  For bases 11-36
  31.  * the letters 'A'-'Z' represent the corresponding values.
  32.  *)
  33.  
  34. Procedure StrtoLong(Var I : LongInt; S : PChar; Radix : Integer);
  35.   begin
  36.     I        := 0;
  37.     While S[0] <> #0 do
  38.       begin
  39.         Case S[0] of '0'..'9' : I := I * Radix + (ord(S[0])-48);
  40.                      'A'..'Z' : I := I * Radix + (ord(S[0])-54);
  41.                      'a'..'z' : I := I * Radix + (ord(S[0])-86);
  42.         Inc(s);
  43.       end;
  44.   end;
  45.  
  46.