Autor Beitrag
Spike
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 69
Erhaltene Danke: 1



BeitragVerfasst: Do 27.06.02 21:59 
Hallo,
ich bin auf der Suche nach einer Funktion zum Umwandeln von Binärdaten ins dezimale Format. Ich könnt's wohl selber schreiben aber ich wollt nur mal wissen ob ich vielleicht nicht gucken kann und es sowas schon gibt. Man muß ja nicht das Rad neu erfinden.
Ich hab nur BinToHex gefunden.
Arakis
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 344



BeitragVerfasst: Do 27.06.02 22:12 
Nimm einfach BinToHex und schleuß dass ganze dann durch dies Funktion hextoint: (Bitte nicht totschlagen, diese Funktion lässt sich garantiert effizienter realisieren :oops: )

ausblenden volle Höhe 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:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
function getnum(h1: String): Integer; 
var 
d1: Integer; 
begin 
  h1:=lowercase(h1); 
  if h1 = '0' Then d1 := 0; 
  if h1 = '1' Then d1 := 1; 
  if h1 = '2' Then d1 := 2; 
  if h1 = '3' Then d1 := 3; 
  if h1 = '4' Then d1 := 4; 
  if h1 = '5' Then d1 := 5; 
  if h1 = '6' Then d1 := 6; 
  if h1 = '7' Then d1 := 7; 
  if h1 = '8' Then d1 := 8; 
  if h1 = '9' Then d1 := 9; 
  if h1 = 'a' Then d1 := 10; 
  if h1 = 'b' Then d1 := 11; 
  if h1 = 'c' Then d1 := 12; 
  if h1 = 'd' Then d1 := 13; 
  if h1 = 'e' Then d1 := 14; 
  if h1 = 'f' Then d1 := 15; 
  result:=d1; 
end; 
 
function hextoint(hex: String): Integer; 
var 
  d: Integer; 
  i: Integer; 
  dez: Integer; 
  h: String; 
  dd: Integer; 
begin 
  i:=1; 
  dez:=0; 
  while i < length(hex)+1 do 
  begin 
    h:=copy(hex, i, 1); 
    d:=getnum(h); 
    dd:=d*round(power(16, length(hex)-i)); 
    dez:=dez+dd; 
    inc(i); 
  end; 
  result:=dez; 
end; 
 
 
procedure TForm1.Button1Click(Sender: TObject); 
 
begin 
  showmessage(inttostr(hextoint('40f'))); 
end;


Bis dann
user defined image

_________________
Mit dem Computer löst man Probleme, die man ohne ihn nicht hätte.
Entwickler von SpaceTrek: The New Empire - Siehe Hompage!
Spike Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 69
Erhaltene Danke: 1



BeitragVerfasst: Do 27.06.02 22:31 
Danke für den Vorschlag. Ich habs nun doch mal direkt gemacht:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
function bin2int(bin : string) : integer;
  var a,z : integer;
begin
  result := 0;
  a := 1;
  for z := length(bin) downto 1 do  begin
    if bin[z]='1' then inc(result,a);
    a := a * 2;
  end;
end;


Zuletzt bearbeitet von Spike am Fr 28.06.02 12:49, insgesamt 3-mal bearbeitet
Arakis
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 344



BeitragVerfasst: Do 27.06.02 22:33 
Wie heißt es denn noch mal? "Alle Wege führen nach Rom". Die einen halt über Umwege :roll:

Bis dann
user defined image

_________________
Mit dem Computer löst man Probleme, die man ohne ihn nicht hätte.
Entwickler von SpaceTrek: The New Empire - Siehe Hompage!
Pit
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 160



BeitragVerfasst: Fr 28.06.02 01:21 


Zuletzt bearbeitet von Pit am Sa 05.10.02 06:22, insgesamt 1-mal bearbeitet
Spike Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 69
Erhaltene Danke: 1



BeitragVerfasst: Fr 28.06.02 11:09 
also ich denke da liegt eine Verwechslung vor. Eine Dual bzw. Binärzahl ist laut Definition:
Zitat:
eine aus einer oder mehreren Binärziffern (0 oder 1) bestehende Zahl. Jedes weitere Bit verdoppelt die Anzahl der möglichen numerischen Kombinationen.

und die gewünschte Dezimalzahl ist eine Zahl auf der Basis von 10 - also schon eine Integer bzw. Realzahl. Und genau diese Umwandlung nimmt meine Prozedure vor. Wie man die Binärzahl darstellt, ob als String oder Array das ist natürlich Geschmacksache.
Allerdings hast du recht, negative Vorzeichen müssten noch berücksichtigt werden.
Pit
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 160



BeitragVerfasst: Fr 28.06.02 12:38 


Zuletzt bearbeitet von Pit am Sa 05.10.02 06:23, insgesamt 1-mal bearbeitet
Spike Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 69
Erhaltene Danke: 1



BeitragVerfasst: Fr 28.06.02 12:56 
also 1. du hast recht - man muß natürlich andersrum zählen -ich habs oben mal verbessert - nun ist 0011 auch 3

aber ansonsten muß ich widersprechen denn natürlich kann man bei einem PC alles bis zur binären Darstellung zurückverfolgen. Aber das war nicht Ziel der Funktion sondern ich wollte einen String der eine binäre Zeichenfolge (also Nullen und Einsen) darstellt in eine dezimale Zahl umwandel. Nicht mehr und nicht weniger.
Und das der PC diese Zahl intern wieder binär speichert usw. spielt ja wohl dabei überhaupt keine Rolle.
Pit
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 160



BeitragVerfasst: Fr 28.06.02 13:19 


Zuletzt bearbeitet von Pit am Sa 05.10.02 06:23, insgesamt 1-mal bearbeitet
Spike Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 69
Erhaltene Danke: 1



BeitragVerfasst: Fr 28.06.02 13:27 
Pit hat folgendes geschrieben:
Spike hat folgendes geschrieben:
ich bin auf der Suche nach einer Funktion zum Umwandeln von Binärdaten ins dezimale Format.

Das wäre IntToStr.


soviel zum Thema Grundlagenwissen. Ich für mich schließe das Thema damit ab, da Du es jetzt persönlich nimmst.
Pit
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 160



BeitragVerfasst: Fr 28.06.02 14:19