Advertisement
HwapX

Base converter

Aug 9th, 2014
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.95 KB | None | 0 0
  1. Program HelloWorld(output);
  2.  
  3. {$mode delphi}
  4.  
  5. uses SysUtils;
  6.  
  7. const
  8.   BaseDigitsLower: string = '0123456789abcdefghijklmnopqrstuvwxyz';
  9.   BaseDigitsUpper: string = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  10.  
  11. var
  12.   BaseDigits: string = '0123456789abcdefghijklmnopqrstuvwxyz';
  13. //function IntToBase(Value: Int64; const Base: Byte; const BaseChars: string{ = BaseDigitsUpper}): string; overload;
  14. //function IntToBase(const Value: Int64; const BaseChars: string): string; overload;
  15.  
  16. function IntToBase(Value: Int64; const Base: Byte; const BaseChars: string): string; overload;
  17. begin
  18.   Result := '';
  19.  
  20.   if Length(BaseChars) < Base then
  21.     Raise Exception.Create('BaseChars size is lower than base');
  22.    
  23.   if Base = 1 then
  24.     Exit(StringOfChar(BaseChars[1], Value));
  25.    
  26.   repeat
  27.     Result := BaseChars[(Value mod Base) + 1] + Result;
  28.     Value := Value div Base;
  29.   until Value = 0;
  30. end;
  31.  
  32. function IntToBase(Value: Int64; const BaseChars: string): string; overload;
  33. begin
  34.   Result := IntToBase(Value, Length(BaseChars), BaseChars);
  35. end;
  36.  
  37. function TryBaseToInt(Str: string; const Base: Byte; BaseChars: string; var O: Int64; const IgnoreCase: Boolean = False): Boolean; overload;
  38. var
  39.   I: Integer;
  40.   D: Integer;
  41. begin
  42.   O := 0;
  43.   Result := False;
  44.  
  45.   if (Base = 1) and (Length(Str) = 0) then
  46.     Exit(True);
  47.  
  48.   if (Length(Str) = 0) or (Length(BaseChars) < Base) then
  49.     Exit(False);
  50.    
  51.   if IgnoreCase then
  52.   begin
  53.     Str       := LowerCase(Str);
  54.     BaseChars := LowerCase(BaseChars);
  55.   end;
  56.  
  57.   for I := 1 to Length(Str) do
  58.   begin
  59.     D := Pos(Str[I], BaseChars);
  60.    
  61.     if D <> 0 then
  62.       if Base = 1 then
  63.         Inc(O)
  64.       else
  65.         O := O * Base + (D-1)
  66.     else
  67.       Exit(False);
  68.   end;
  69.  
  70.   Result := True;
  71. end;
  72.  
  73. function BaseToInt(const Str: string; const Base: Byte; const BaseChars: string; const IgnoreCase: Boolean = False): Int64; overload;
  74. begin
  75.   if not TryBaseToInt(Str, Base, BaseChars, Result, IgnoreCase) then
  76.     Raise Exception.Create('Cant convert');
  77. end;
  78.  
  79. function BaseToInt(const Str: string; const BaseChars: string; const IgnoreCase: Boolean = False): Int64; overload;
  80. begin
  81.   Result := BaseToInt(Str, Length(BaseChars), BaseChars, IgnoreCase);
  82. end;
  83.  
  84. function DigitoVerificador(Str: string): Char;
  85. var
  86.   I: Integer;
  87.   Sum: Integer;
  88.   D: Integer;
  89. begin
  90.   if Length(Str) <> 7 then
  91.     Exit(#0);
  92.    
  93.   D := BaseToInt(Str[7], BaseDigitsUpper);
  94.   if D = 0 then
  95.     D := 1;
  96.  
  97.   Sum := 0;
  98.   for I := 1 to 3 do
  99.     Inc(Sum, BaseToInt(Str[I], BaseDigitsUpper) * BaseToInt(Str[7-I], BaseDigitsUpper));
  100.  
  101.   Result := IntToBase(Sum mod D, BaseDigitsUpper)[1];
  102. end;
  103.  
  104. var
  105.   Dec: Int64;
  106.   B36: string;
  107.   Base: Integer;
  108. begin
  109.   Base := 36;
  110.   Dec := 10;
  111.   B36 := IntToBase(Dec, Base, BaseDigitsUpper);
  112.   B36 := StringOfChar('0', 7-Length(B36)) + B36;
  113.   writeln(Dec, slineBreak, B36, sLineBreak, BaseToInt(B36, Base, BaseDigitsUpper));
  114.  
  115.   writeln(B36, DigitoVerificador(B36));
  116. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement