Autor Beitrag
Blade
Hält's aus hier
Beiträge: 1



BeitragVerfasst: Mo 27.09.04 20:15 
Hi!
Ich habe eine ganz wichtige Frage an euch: Wie kann ich eine Zahl in verschiedene Zahlensysteme umwandeln. Wenn ich z.B. die Zahl 144 im 10ner Zahlensystem habe, wie kann ich sie dann z.B. in ein Hexerdezimal System umwandeln, also in ein 16ner System? Am besten wäre es, wenn das ganze durch eine Prozedur funktionieren würde, sodass man z.B. in der Prozedur die Zahl und das das in das zu übertragende System eingeben kann. Ist wirklich wichtig. Danke


Blade
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Mo 27.09.04 20:21 
von 10ner in 16ner -> inttohex(124)
bzw umgekehrt hextoint('$12ab10FF');

ansonsten musste halt bisl umrechnen
.Chef
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1112



BeitragVerfasst: Mo 27.09.04 20:24 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
function convnumsystems(zahl : integer;zielsystem : Byte) : string;
const
  ziffern : array[0..15of Char =
    ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
var
  a : Byte;
  b : Integer;
begin
  Result:='';
  b:=Zahl;
  while b > 0 do
  begin
    Result:=ziffern[b mod zielsystem]+Result;
    b:=b div zielsystem;
  end;
end;

Ungetestet, für alle Systeme zwischen Binär und Hex.

Gruß,
Jörg

_________________
Die Antworten auf die 5 häufigsten Fragen:
1. Copy(), Pos(), Length() --- 2. DoubleBuffered:=True; --- 3. Application.ProcessMessages bzw. TThread --- 4. ShellExecute() --- 5. Keine Vergleiche von Real-Typen mit "="!
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Mo 27.09.04 20:44 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
function umwandeln(s: string; von, zu: integer): string;
var i: integer;
    zahl: integer;
    wert: string;
    error: boolean;
begin
  wert := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  error := false;
  for i := 1 to length(s) do
    if (pos(s[i],wert)-1) > von then error := true;
  if not error then
  begin
    zahl := 0;
    for i := 1 to length(s) do
    begin
      inc(zahl,pos(s[i],wert)-1);
      zahl := zahl*von;
    end;
    zahl := zahl div von;
    result := '';
    while zahl > 0 do
    begin
      result := wert[(zahl mod zu)+1]+ result;
      zahl := zahl div zu;
    end;
  end else
  messagebox(0,'wert kann nicht im angegeben zahlensystem liegen!',nil,0);
end;


damit kann man dann von jedem system in jedes andere umrechnen
hab aber nicht darauf geachtet, das z.b. wenn 'AB' angegeben wird und von das 10ner system ist das es die zahl dort gar nicht gibt, sollte man noch einbauen

EDIT: problem gelöst :>
axis
Hält's aus hier
Beiträge: 4



BeitragVerfasst: Fr 20.05.05 23:28 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
function hexazahl (zahl:longword):string;
var rest:byte;
    ergebnis:string;
begin
  result:='';
  repeat
    rest:=zahl mod 16;
    zahl:=zahl div 16;
    case rest of
    0..9: ergebnis:=inttostr(rest);
    10:   ergebnis:='A';
    11:   ergebnis:='B';
    12:   ergebnis:='C';
    13:   ergebnis:='D';
    14:   ergebnis:='E';
    15:   ergebnis:='F';
    end;
    result:=ergebnis+result;
  until zahl=0;
end;


Moderiert von user profile iconraziel: Delphi-Tags hinzugefügt.