Advertisement
wojtas626

[PAS] Any to any number system

Jan 18th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.13 KB | None | 0 0
  1. program project1;
  2.  
  3. uses
  4.     crt, DOS;
  5.  
  6. const Digits: String='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  7.  
  8. function AnyToDec(inputString: String; numberSystem: Integer): Int64;
  9. var
  10.    X, i: Integer;
  11. begin
  12.      Result := 0;
  13.  
  14.      for i := 1 to Length(inputString) do
  15.      begin
  16.           X := Pos(inputString[i], Digits) - 1;
  17.           Result := Result*numberSystem + X;
  18.      end;
  19. end;
  20.  
  21. function DecToAny(input: Int64; numberSystem: Integer): String;
  22. var
  23.    X: Integer;
  24. begin
  25.      Result := '';
  26.  
  27.      repeat
  28.            X := input mod numberSystem;
  29.            input := input div numberSystem;
  30.            Result := Digits[X + 1] + Result;
  31.      until (input = 0) ;
  32. end;
  33.  
  34. var
  35.    inputSystem, outputSystem: Int64;
  36.    inputValue: String;
  37.  
  38. begin
  39.      write('system z ktorego bedziesz konwertowal: ');
  40.      readLn(inputSystem);
  41.      write('system do ktorego bedziesz konwertowal: ');
  42.      readLn(outputSystem);
  43.  
  44.      write('Wprowadz liczbe do konwersji w systemie z ktorego chcesz konwertowac: ');
  45.      readLn(inputValue);
  46.  
  47.      writeln( DecToAny( AnyToDec( inputValue, inputSystem ), outputSystem ) );
  48.  
  49.  
  50.      ReadKey;
  51.  
  52. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement