Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program HelloWorld(output);
- {$mode delphi}
- uses SysUtils;
- const
- BaseDigitsLower: string = '0123456789abcdefghijklmnopqrstuvwxyz';
- BaseDigitsUpper: string = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
- var
- BaseDigits: string = '0123456789abcdefghijklmnopqrstuvwxyz';
- //function IntToBase(Value: Int64; const Base: Byte; const BaseChars: string{ = BaseDigitsUpper}): string; overload;
- //function IntToBase(const Value: Int64; const BaseChars: string): string; overload;
- function IntToBase(Value: Int64; const Base: Byte; const BaseChars: string): string; overload;
- begin
- Result := '';
- if Length(BaseChars) < Base then
- Raise Exception.Create('BaseChars size is lower than base');
- if Base = 1 then
- Exit(StringOfChar(BaseChars[1], Value));
- repeat
- Result := BaseChars[(Value mod Base) + 1] + Result;
- Value := Value div Base;
- until Value = 0;
- end;
- function IntToBase(Value: Int64; const BaseChars: string): string; overload;
- begin
- Result := IntToBase(Value, Length(BaseChars), BaseChars);
- end;
- function TryBaseToInt(Str: string; const Base: Byte; BaseChars: string; var O: Int64; const IgnoreCase: Boolean = False): Boolean; overload;
- var
- I: Integer;
- D: Integer;
- begin
- O := 0;
- Result := False;
- if (Base = 1) and (Length(Str) = 0) then
- Exit(True);
- if (Length(Str) = 0) or (Length(BaseChars) < Base) then
- Exit(False);
- if IgnoreCase then
- begin
- Str := LowerCase(Str);
- BaseChars := LowerCase(BaseChars);
- end;
- for I := 1 to Length(Str) do
- begin
- D := Pos(Str[I], BaseChars);
- if D <> 0 then
- if Base = 1 then
- Inc(O)
- else
- O := O * Base + (D-1)
- else
- Exit(False);
- end;
- Result := True;
- end;
- function BaseToInt(const Str: string; const Base: Byte; const BaseChars: string; const IgnoreCase: Boolean = False): Int64; overload;
- begin
- if not TryBaseToInt(Str, Base, BaseChars, Result, IgnoreCase) then
- Raise Exception.Create('Cant convert');
- end;
- function BaseToInt(const Str: string; const BaseChars: string; const IgnoreCase: Boolean = False): Int64; overload;
- begin
- Result := BaseToInt(Str, Length(BaseChars), BaseChars, IgnoreCase);
- end;
- function DigitoVerificador(Str: string): Char;
- var
- I: Integer;
- Sum: Integer;
- D: Integer;
- begin
- if Length(Str) <> 7 then
- Exit(#0);
- D := BaseToInt(Str[7], BaseDigitsUpper);
- if D = 0 then
- D := 1;
- Sum := 0;
- for I := 1 to 3 do
- Inc(Sum, BaseToInt(Str[I], BaseDigitsUpper) * BaseToInt(Str[7-I], BaseDigitsUpper));
- Result := IntToBase(Sum mod D, BaseDigitsUpper)[1];
- end;
- var
- Dec: Int64;
- B36: string;
- Base: Integer;
- begin
- Base := 36;
- Dec := 10;
- B36 := IntToBase(Dec, Base, BaseDigitsUpper);
- B36 := StringOfChar('0', 7-Length(B36)) + B36;
- writeln(Dec, slineBreak, B36, sLineBreak, BaseToInt(B36, Base, BaseDigitsUpper));
- writeln(B36, DigitoVerificador(B36));
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement