Advertisement
HEX0x29A

Convert Scale Of Notation

Oct 15th, 2014
1,184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.84 KB | None | 0 0
  1. function ConvertScaleOfNotation(const InputValue : LPCSTR;
  2.                                 const InputScale : BYTE;
  3.                                 const OutputValue: LPCSTR;
  4.                                 const OutputSize : DWORD;
  5.                                 const OutputScale: BYTE): BOOL;
  6. const
  7.   SYMBOLS : AnsiString  = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  8.   RANGE   : set of BYTE = [2..36];
  9. var
  10.   i, d       : integer;
  11.   szInputVal : AnsiString;
  12.   szOutputVal: AnsiString;
  13. begin
  14.   szInputVal := AnsiString(InputValue);
  15.   Result := (Length(szInputVal) > 0) and
  16.             (InputScale in RANGE) and
  17.             (OutputScale in RANGE) and
  18.             (OutputValue <> nil) and
  19.             (OutputSize >= 2);
  20.   if not Result then
  21.     Exit;
  22.   for i := Low(szInputVal) to High(szInputVal) do
  23.   begin
  24.     if szInputVal[i] in ['a'..'z'] then
  25.       szInputVal[i] := AnsiChar(Ord(szInputVal[i]) - $20);
  26.     if (Pos(szInputVal[i], SYMBOLS) > InputScale)
  27.     or (Pos(szInputVal[i], SYMBOLS) = 0) then
  28.     begin
  29.       Result := False;
  30.       Exit;
  31.     end;
  32.   end;
  33.   szOutputVal := '';
  34.   d := 0;
  35.   for i := Low(szInputVal) to Pred(High(szInputVal)) do
  36.     d := (d + Pred(Pos(szInputVal[i], SYMBOLS))) * InputScale;
  37.   d := d + Pred(Pos(szInputVal[Length(szInputVal)], SYMBOLS));
  38.   while d > Pred(OutputScale) do
  39.   begin
  40.     szOutputVal := SYMBOLS[Succ(d mod OutputScale)] + szOutputVal;
  41.     d := (d - (d mod OutputScale)) div OutputScale;
  42.   end;
  43.   szOutputVal := SYMBOLS[Succ(d)] + szOutputVal;
  44.   while szOutputVal[1] = '0' do
  45.     Delete(szOutputVal, 1, 1);
  46.   if (Length(szOutputVal) = 0)
  47.   or (Pos(szOutputVal[1], SYMBOLS) = 0) then
  48.     szOutputVal := '0';
  49.   Result := (DWORD(Length(szOutputVal)) <= DWORD(Pred(OutputSize)));
  50.   ZeroMemory(OutputValue, OutputSize);
  51.   if Result then
  52.     lstrcpyA(OutputValue, @szOutputVal[1]);
  53. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement