Autor Beitrag
Udontknow
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2596

Win7
D2006 WIN32, .NET (C#)
BeitragVerfasst: Di 27.05.03 10:21 
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:
function HexValue(BinValues:String):Integer; 
//gibt den Wert eines maximal 4 stelligen String mit Binäziffern zurück
var i:integer; 
begin 
  //Prüfung: Länge höchstens 4 (ansonsten nicht in einzelnem Hex-Zeichen darstellbar)
  if Length(BinValues)>4 then 
    raise Exception.Create('Überlauf'); 

  //String enthält Binärzahl?
  if not IsBinaryString(BinValues) then
    raise Exception.Create('Keine Binärzahl'); 

  //Wert zunächst null
  Result:=0; 
  //Alle Binärziffern durchgehen, bei der hochwertigsten Ziffer beginnen
  for i:=1 to Length(BinValues) do 
  begin 
    //bisheriges Ergebnis verdoppeln (<=> Shift left)
    Result:=Result*2; 
    //Je nach Binärziffer entweder 0 oder 1 zum aktuellen Ergebnis addieren
    Result:=Result+StrToInt(BinValues[i]); 
  end; 
end;


Cu,
Udontknow


Zuletzt bearbeitet von Udontknow am Di 27.05.03 10:28, insgesamt 2-mal bearbeitet
Udontknow
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2596

Win7
D2006 WIN32, .NET (C#)
BeitragVerfasst: Di 27.05.03 10:25 
Hier eine bessere Funktion zur Validierung.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
function IsBinaryString(Str:String):Boolean;
var i:integer;
begin
  //Länge 0? Keine Zahl, Abbruch.
  if Length(Str)=0 then
    Result:=False;
    exit;
  end;

  Result:=True;
  //Alle Zeichen durchgehen
  for i:=1 to Length(Str) do
    //Irgendein Zeichen nicht 0 oder 1? Keine Binärzahl, Abbruch.
    if not (Str[i] in ('0','1')) then
    begin
      Result:=False;
      exit;
    end;
end;


Cu,
Udontknow
Melanie
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: Do 29.05.03 09:51 
Titel: Bintohexa
Hallo ,ich habe mal die folgende Funktion ausprobiert.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
function BinToHexa(text:string):string; 
  const 
    symbole='0123456789ABCDEF'; 
var BinValues:String; 
var i:integer; 
begin 
  Result:=''; 
  //Start bei letzter Ziffer 
  i:=length(text); 
  repeat 
    BinValues:=''; 
    repeat 
      //Aktuelle binäre Ziffer nehmen 
      BinValues:=Text[i]+BinValues; 
      //nächste Ziffer (Position ändern) 
      Dec(i); 
    until (i=0) or (Length(BinValues)=4); 
    //Entsprechendes Hex-Zeichen hinzufügen 
    Result:=Symbole[HexValue(BinValues)+1]+Result; 
  until i=0; 
end;


'HexValue' ist ja nicht deklariert,es kommt ein Compilerfehler.
Meine Frage:Für was steht den 'HexValue'in dieser Funktion den eigentlich?

Mfg Melly

Moderiert von user profile iconNarses: Code- durch Delphi-Tags ersetzt
Anja Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 42

WIN xp

BeitragVerfasst: Do 29.05.03 10:25 
du musst hexvalue in einer extra funktion deklarieren!
Melanie
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: Fr 30.05.03 08:14 
Danke Anja,die Funktion ist ja aufgeführt,habe ich nur überlesen.

cu Melly :roll: