Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ConvertScaleOfNotation(const InputValue : LPCSTR;
- const InputScale : BYTE;
- const OutputValue: LPCSTR;
- const OutputSize : DWORD;
- const OutputScale: BYTE): BOOL;
- const
- SYMBOLS : AnsiString = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
- RANGE : set of BYTE = [2..36];
- var
- i, d : integer;
- szInputVal : AnsiString;
- szOutputVal: AnsiString;
- begin
- szInputVal := AnsiString(InputValue);
- Result := (Length(szInputVal) > 0) and
- (InputScale in RANGE) and
- (OutputScale in RANGE) and
- (OutputValue <> nil) and
- (OutputSize >= 2);
- if not Result then
- Exit;
- for i := Low(szInputVal) to High(szInputVal) do
- begin
- if szInputVal[i] in ['a'..'z'] then
- szInputVal[i] := AnsiChar(Ord(szInputVal[i]) - $20);
- if (Pos(szInputVal[i], SYMBOLS) > InputScale)
- or (Pos(szInputVal[i], SYMBOLS) = 0) then
- begin
- Result := False;
- Exit;
- end;
- end;
- szOutputVal := '';
- d := 0;
- for i := Low(szInputVal) to Pred(High(szInputVal)) do
- d := (d + Pred(Pos(szInputVal[i], SYMBOLS))) * InputScale;
- d := d + Pred(Pos(szInputVal[Length(szInputVal)], SYMBOLS));
- while d > Pred(OutputScale) do
- begin
- szOutputVal := SYMBOLS[Succ(d mod OutputScale)] + szOutputVal;
- d := (d - (d mod OutputScale)) div OutputScale;
- end;
- szOutputVal := SYMBOLS[Succ(d)] + szOutputVal;
- while szOutputVal[1] = '0' do
- Delete(szOutputVal, 1, 1);
- if (Length(szOutputVal) = 0)
- or (Pos(szOutputVal[1], SYMBOLS) = 0) then
- szOutputVal := '0';
- Result := (DWORD(Length(szOutputVal)) <= DWORD(Pred(OutputSize)));
- ZeroMemory(OutputValue, OutputSize);
- if Result then
- lstrcpyA(OutputValue, @szOutputVal[1]);
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement