Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- implementation
- const
- alph36 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
- function CharToLongNumber(val: char; base: TBase): TLongNumber;
- var num: int64;
- begin
- num := Pos(val, alph36);
- if (num > 0) and (num-1 < base) then begin
- Result := num-1;
- end
- else
- raise Exception.Create('Invalid char ''' + val + '''');
- end;
- function Invers(Res: TLongNumber): TLongNumber;
- var
- arr: TLongNumber;
- i: integer;
- begin
- SetLength(arr.dig, high(res.dig)+1);
- for i := 0 to high(res.dig) do begin
- arr.dig[high(arr.dig)-i] := res.dig[i];
- end;
- arr.inv := res.inv;
- Result := arr;
- end;
- function fromString(const str: AnsiString; base: TBase): TLongNumber;
- var i, n: integer;
- res, c: TLongNumber;
- begin
- res := 0;
- if str = '' then exit(res);
- if (str = '0') or (str = '-0') then res.inv := true;
- if (str[1] = '-') then begin
- res.inv := false;
- n := 2;
- end
- else begin
- res.inv := true;
- n := 1;
- end;
- while((str[n] = '0') and (n < Length(str))) do n+=1;
- for i:=n to length(str) do begin
- c := charToLongNumber(str[i], base);
- Res := Res * base + c;
- end;
- if (str[1] = '-') then begin
- res.inv := false;
- end;
- exit(res);
- end;
- function toString(const val: TLongNumber; base: TBase): AnsiString;
- var res, buf: TLongNumber;
- c: longWord;
- begin
- Result := '';
- res := val;
- buf := base;
- while (CompareModule(res,buf)) do begin
- res := divmod(res, base, c);
- Result += alph36[c+1];
- end;
- res := divmod(res, base, c);
- Result += IntToStr(c);
- if (res.inv = false) then begin
- Result += '-';
- end;
- Result := invers_str(result);
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement