Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#16

AW: Problem mit TColor

  Alt 6. Sep 2010, 00:25
Damit das Umrechnen zwischen den einzelnen Zahlensystemen einfacher wird habe ich da mal einen Record zusammengebastelt.
Damit kannst du zwischen den Zahlensystemen 2..36 hin und herrechnen.
Delphi-Quellcode:
unit uBaseConv;

interface

type
  TIntBase = record
  private
    function GetAsBase( Base : Integer ) : string;
    procedure SetAsBase( Base : Integer; const AValue : string );

  public
    Value : Int64;
    property AsBase[ Base : Integer ] : string read GetAsBase write SetAsBase; default;
  end;

function IntToBase( const Value : Int64; const Base : Integer ) : string;
function BaseToInt( const Value : string; const Base : Integer ) : Int64;

implementation

uses
  SysUtils;

function IntToBase( const Value : Int64; const Base : Integer ) : string;
  var
    Val : Int64;
    Res : Byte;
  begin
    if ( Base >= 2 ) and ( Base <= 36 ) then
      begin
        Val := Value;
        while Val > 0 do
          begin
            Res := Val mod Base;
            case Res of
              0 .. 9 :
                Result := Chr( Ord( '0' ) + Res ) + Result;
              10 .. 35 :
                Result := Chr( Ord( 'A' ) + Res - 10 ) + Result;
            end;
            Val := Val div Base;
          end;
      end
    else
      raise Exception.CreateFmt( 'Basis %d ausserhalb des gülitigen Bereichs 2..36', [ Base ] );
  end;

function BaseToInt( const Value : string; const Base : Integer ) : Int64;
  var
    idx : Integer;
    pdx : Integer;
  begin
    if ( Base >= 2 ) and ( Base <= 36 ) then
      begin
        Result := 0;
        idx := 1;
        while idx <= Length( Value ) do
          begin
            Result := Result * Base;
            case Value[ idx ] of
              '0' .. '9' :
                pdx := Ord( Value[ idx ] ) - Ord( '0' );
              'A' .. 'Z' :
                pdx := Ord( Value[ idx ] ) - Ord( 'A' ) + 10;
              'a' .. 'z' :
                pdx := Ord( Value[ idx ] ) - Ord( 'a' ) + 10;
            else
              raise Exception.CreateFmt( 'Ungültiges Zeichen im Wert "%s" entdeckt!', [ Value ] );
            end;
            if pdx < Base then
              Result := Result + pdx
            else
              raise Exception.CreateFmt( 'Der Wert "%s" passt nicht zur Basis %d!', [ Value, Base ] );

            idx := idx + 1;
          end;
      end
    else
      raise Exception.CreateFmt( 'Basis %d ausserhalb des gülitigen Bereichs 2..36', [ Base ] );
  end;

{ TIntBase }

function TIntBase.GetAsBase( Base : Integer ) : string;
  begin
    Result := IntToBase( Value, Base );
  end;

procedure TIntBase.SetAsBase( Base : Integer; const AValue : string );
  begin
    Value := BaseToInt( AValue, Base );
  end;

end.
Benutzt wird das dann wie folgt:
Delphi-Quellcode:
var
  MyVal : TIntBase;
begin
  MyVal.Value := 10; // Int64-Wert
  MyVal[ 10 ] := '10'; // Dezimalsystem
  MyVal[ 16 ] := 'A'; // Hex
  MyVal[ 2 ] := '1010'; // Binär

  // Umrechnen von Hexadezimal in Binär

  MyVal[ 16 ] := 'A2F3';
  ShowMessage( MyVal[ 2 ] );

  // Intern rechnen

  MyVal.Value := MyVal.Value * 2;
  
end;
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat