Subversion Repositories delphiutils

Rev

Blame | Last modification | View Log | RSS feed

  1. unit Main;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls;
  8.  
  9. type
  10.   TMainForm = class(TForm)
  11.     Memo1: TMemo;
  12.     Memo2: TMemo;
  13.     Edit1: TEdit;
  14.     Button1: TButton;
  15.     Label1: TLabel;
  16.     Label2: TLabel;
  17.     CheckBox1: TCheckBox;
  18.     procedure Button1Click(Sender: TObject);
  19.   private
  20.     { Private-Deklarationen }
  21.   public
  22.     { Public-Deklarationen }
  23.   end;
  24.  
  25. var
  26.   MainForm: TMainForm;
  27.  
  28. implementation
  29.  
  30. {$R *.dfm}
  31.  
  32. // Future Idea: Any <-> Any Converting
  33.  
  34. function convertToAnyBase(num, base: integer; useAlpha: boolean): String;
  35. var
  36.   q, r: integer;
  37. begin
  38.   result := '';
  39.  
  40.   while true do
  41.   begin
  42.     q := num div base;
  43.     r := num mod base;
  44.  
  45.     if useAlpha and (r <= 35) then
  46.     begin
  47.       if (r < 10) then
  48.       begin
  49.         result := IntToStr(r) + result; // 0..9
  50.       end
  51.       else
  52.       begin
  53.         result := chr(ord('A') + r - 10) + result; // A..Z
  54.       end;
  55.     end
  56.     else
  57.     begin
  58.       result := Format('(%d)%s', [r, result]);
  59.     end;
  60.  
  61.     if q = 0 then
  62.     begin
  63.       break;
  64.     end;
  65.  
  66.     num := q;
  67.   end;
  68. end;
  69.  
  70. procedure TMainForm.Button1Click(Sender: TObject);
  71. var
  72.   i, n, b: integer;
  73.   s: string;
  74. begin
  75.   b := StrToInt(Edit1.Text);
  76.   memo2.Lines.Clear;
  77.   for i := 0 to memo1.Lines.Count - 1 do
  78.   begin
  79.     s := memo1.Lines.Strings[i];
  80.     if (s = '') then
  81.     begin
  82.       memo2.Lines.Add('');
  83.     end
  84.     else
  85.     begin
  86.       n := StrToInt(s);
  87.       memo2.Lines.Add(convertToAnyBase(n, b, CheckBox1.Checked));
  88.     end;
  89.   end;
  90. end;
  91.  
  92. end.
  93.